mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-06 04:42:15 -05:00
Restructure Color constructor to remove need for Color.create().
This commit is contained in:
parent
1cbb303941
commit
2a53dbe60f
2 changed files with 63 additions and 58 deletions
|
@ -449,7 +449,7 @@ var Raster = this.Raster = Item.extend(/** @lends Raster# */{
|
||||||
components = [0, 0, 0, 0];
|
components = [0, 0, 0, 0];
|
||||||
for (var i = 0; i < 4; i++)
|
for (var i = 0; i < 4; i++)
|
||||||
components[i] = pixels[i] / 255;
|
components[i] = pixels[i] / 255;
|
||||||
return Color.create('rgb', components);
|
return new Color('rgb', components);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -443,48 +443,61 @@ var Color = this.Color = Base.extend(new function() {
|
||||||
read = 0,
|
read = 0,
|
||||||
type,
|
type,
|
||||||
components,
|
components,
|
||||||
alpha;
|
alpha,
|
||||||
|
values;
|
||||||
|
// If first argument is an array, replace arguments with it.
|
||||||
if (Array.isArray(arg)) {
|
if (Array.isArray(arg)) {
|
||||||
args = arg;
|
args = arg;
|
||||||
arg = args[0];
|
arg = args[0];
|
||||||
}
|
}
|
||||||
// Try type arg first
|
// First see if it's a type string argument, and if so, set it and
|
||||||
if (typeof arg === 'string' && arg in types) {
|
// shift it out of the arguments list.
|
||||||
|
var argType = arg != null && typeof arg;
|
||||||
|
if (argType === 'string' && arg in types) {
|
||||||
type = arg;
|
type = arg;
|
||||||
if (this._read)
|
arg = args[1];
|
||||||
read = 1; // will be increased below
|
if (Array.isArray(arg)) {
|
||||||
// Shift type out of the arguments list, and process normally.
|
// Internal constructor that is called with the following
|
||||||
args = slice.call(args, 1);
|
// arguments, without parsing: (type, componets, alpha)
|
||||||
arg = args[0];
|
components = arg;
|
||||||
|
alpha = args[2];
|
||||||
|
} else {
|
||||||
|
// For deserialization, shift out and process normally.
|
||||||
|
if (this._read)
|
||||||
|
read = 1; // Will be increased below
|
||||||
|
// Shift type out of the arguments, and process normally.
|
||||||
|
args = slice.call(args, 1);
|
||||||
|
argType = typeof arg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var argType = arg != null && typeof arg,
|
if (!components) {
|
||||||
|
// Determine if there is a values array
|
||||||
values = argType === 'number'
|
values = argType === 'number'
|
||||||
? args
|
? args
|
||||||
// Do not use Array.isArray() to also support arguments list
|
// Do not use Array.isArray() to also support arguments
|
||||||
: argType === 'object' && arg.length != null
|
: argType === 'object' && arg.length != null
|
||||||
? arg
|
? arg
|
||||||
: null;
|
: null;
|
||||||
// The various branches below produces a values array if the values
|
// The various branches below produces a values array if the
|
||||||
// still need parsing, and a components array if they are already
|
// values still need parsing, and a components array if they are
|
||||||
// parsed.
|
// already parsed.
|
||||||
if (values) {
|
if (values) {
|
||||||
if (!type)
|
if (!type)
|
||||||
// type = values.length >= 4
|
// type = values.length >= 4
|
||||||
// ? 'cmyk'
|
// ? 'cmyk'
|
||||||
// : values.length >= 3
|
// : values.length >= 3
|
||||||
type = values.length >= 3
|
type = values.length >= 3
|
||||||
? 'rgb'
|
? 'rgb'
|
||||||
: 'gray';
|
: 'gray';
|
||||||
var length = types[type].length;
|
var length = types[type].length;
|
||||||
alpha = values[length];
|
alpha = values[length];
|
||||||
if (this._read)
|
if (this._read)
|
||||||
read += values === arguments
|
read += values === arguments
|
||||||
? length + (alpha != null ? 1 : 0)
|
? length + (alpha != null ? 1 : 0)
|
||||||
: 1;
|
: 1;
|
||||||
if (values.length > length)
|
if (values.length > length)
|
||||||
values = slice.call(values, 0, length);
|
values = slice.call(values, 0, length);
|
||||||
} else {
|
} else if (argType === 'string') {
|
||||||
if (argType === 'string') {
|
|
||||||
components = arg.match(/^#[0-9a-f]{3,6}$/i)
|
components = arg.match(/^#[0-9a-f]{3,6}$/i)
|
||||||
? hexToRgb(arg)
|
? hexToRgb(arg)
|
||||||
: nameToRgb(arg);
|
: nameToRgb(arg);
|
||||||
|
@ -494,6 +507,15 @@ var Color = this.Color = Base.extend(new function() {
|
||||||
type = arg._type;
|
type = arg._type;
|
||||||
components = arg._components.slice();
|
components = arg._components.slice();
|
||||||
alpha = arg._alpha;
|
alpha = arg._alpha;
|
||||||
|
if (type === 'gradient') {
|
||||||
|
// Clone all points, since they belong to the other
|
||||||
|
// color already.
|
||||||
|
for (var i = 1, l = components.length; i < l; i++) {
|
||||||
|
var point = components[i];
|
||||||
|
if (point)
|
||||||
|
components[i] = point.clone();
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if (arg._class === 'Gradient') {
|
} else if (arg._class === 'Gradient') {
|
||||||
type = 'gradient';
|
type = 'gradient';
|
||||||
values = args;
|
values = args;
|
||||||
|
@ -540,7 +562,9 @@ var Color = this.Color = Base.extend(new function() {
|
||||||
if (type === 'gradient')
|
if (type === 'gradient')
|
||||||
this._id = ++Base._uid;
|
this._id = ++Base._uid;
|
||||||
if (!components) {
|
if (!components) {
|
||||||
// Produce a components array now, and parse values
|
// Produce a components array now, and parse values. Even if no
|
||||||
|
// values are defined, parsers are still called to produce
|
||||||
|
// defaults.
|
||||||
this._components = components = [];
|
this._components = components = [];
|
||||||
var parse = parsers[this._type];
|
var parse = parsers[this._type];
|
||||||
for (var i = 0, l = parse.length; i < l; i++) {
|
for (var i = 0, l = parse.length; i < l; i++) {
|
||||||
|
@ -577,8 +601,7 @@ var Color = this.Color = Base.extend(new function() {
|
||||||
* @return {Color} a copy of the color object
|
* @return {Color} a copy of the color object
|
||||||
*/
|
*/
|
||||||
clone: function() {
|
clone: function() {
|
||||||
return Color.create(this._type, this._components.slice(),
|
return new Color(this._type, this._components.slice(), this._alpha);
|
||||||
this._alpha);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -597,7 +620,7 @@ var Color = this.Color = Base.extend(new function() {
|
||||||
},
|
},
|
||||||
|
|
||||||
convert: function(type) {
|
convert: function(type) {
|
||||||
return Color.create(type, this._convert(type), this._alpha);
|
return new Color(type, this._convert(type), this._alpha);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -997,24 +1020,6 @@ var Color = this.Color = Base.extend(new function() {
|
||||||
// Export for backward compatibility code below.
|
// Export for backward compatibility code below.
|
||||||
_types: types,
|
_types: types,
|
||||||
|
|
||||||
create: function(type, components, alpha) {
|
|
||||||
var color = Base.create(Color);
|
|
||||||
color._type = type;
|
|
||||||
color._components = components;
|
|
||||||
color._alpha = alpha;
|
|
||||||
if (type === 'gradient') {
|
|
||||||
// Make sure gradients always have an id
|
|
||||||
color._id = ++Base._uid;
|
|
||||||
// Clone all points:
|
|
||||||
for (var i = 1, l = components.length; i < l; i++) {
|
|
||||||
var point = components[i];
|
|
||||||
if (point)
|
|
||||||
components[i] = point.clone();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return color;
|
|
||||||
},
|
|
||||||
|
|
||||||
random: function() {
|
random: function() {
|
||||||
var random = Math.random;
|
var random = Math.random;
|
||||||
return new Color(random(), random(), random());
|
return new Color(random(), random(), random());
|
||||||
|
|
Loading…
Reference in a new issue