mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -05:00
parent
a36c1bac75
commit
a8f78859cb
2 changed files with 15 additions and 8 deletions
|
@ -62,14 +62,18 @@ var Color = Base.extend(new function() {
|
||||||
var match = string.match(/^#(\w{1,2})(\w{1,2})(\w{1,2})$/),
|
var match = string.match(/^#(\w{1,2})(\w{1,2})(\w{1,2})$/),
|
||||||
type = 'rgb',
|
type = 'rgb',
|
||||||
components;
|
components;
|
||||||
if (match) {
|
if (/^#[A-Fa-f0-9]+$/.test( string )) {
|
||||||
// Hex
|
// HEX / HEX+A
|
||||||
components = [0, 0, 0];
|
var base = string.replace(/^#/,'');
|
||||||
for (var i = 0; i < 3; i++) {
|
var size = base.length;
|
||||||
var value = match[i + 1];
|
components = base.split( size <= 4 ? /(.)/ : /(..)/ );
|
||||||
components[i] = parseInt(value.length == 1
|
components = components.filter(Boolean).map(function(x) {
|
||||||
? value + value : value, 16) / 255;
|
return parseInt(size <= 4 ? x + x : x, 16) / 255;
|
||||||
}
|
});
|
||||||
|
|
||||||
|
if ( !components[0] ) components[0] = 0;
|
||||||
|
if ( !components[1] ) components[1] = 0;
|
||||||
|
if ( !components[2] ) components[2] = 0;
|
||||||
} else if (match = string.match(/^(rgb|hsl)a?\((.*)\)$/)) {
|
} else if (match = string.match(/^(rgb|hsl)a?\((.*)\)$/)) {
|
||||||
// RGB / RGBA or HSL / HSLA
|
// RGB / RGBA or HSL / HSLA
|
||||||
type = match[1];
|
type = match[1];
|
||||||
|
|
|
@ -66,6 +66,9 @@ test('Creating Colors', function() {
|
||||||
equals(new Color('#ff0000'), new Color(1, 0, 0),
|
equals(new Color('#ff0000'), new Color(1, 0, 0),
|
||||||
'Color from hex string');
|
'Color from hex string');
|
||||||
|
|
||||||
|
equals(new Color('#ff000099'), new Color(1, 0, 0, .6),
|
||||||
|
'Color from hex code with alpha');
|
||||||
|
|
||||||
equals(new Color('rgb(255, 0, 0)'), new Color(1, 0, 0),
|
equals(new Color('rgb(255, 0, 0)'), new Color(1, 0, 0),
|
||||||
'Color from rgb() string');
|
'Color from rgb() string');
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue