mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-04 03:45:58 -05:00
Fix issues with new Color serialization.
This commit is contained in:
parent
cf21da75fc
commit
01673d675d
2 changed files with 37 additions and 26 deletions
|
@ -219,30 +219,45 @@ var Color = this.Color = Base.extend(new function() {
|
|||
|
||||
initialize: function(arg) {
|
||||
// We are storing color internally as an array of components
|
||||
var argType = arg != null && typeof arg,
|
||||
type,
|
||||
var slice = Array.prototype.slice,
|
||||
argType = arg != null && typeof arg,
|
||||
components = argType === 'number'
|
||||
? arguments
|
||||
: Array.isArray(arg)
|
||||
? arg
|
||||
: [],
|
||||
? arg
|
||||
: null,
|
||||
read = 0,
|
||||
type,
|
||||
alpha;
|
||||
if (components.length > 0) {
|
||||
// type = arg.length >= 4
|
||||
// ? 'cmyk'
|
||||
// : arg.length >= 3
|
||||
type = components.length >= 3
|
||||
? 'rgb'
|
||||
: 'gray';
|
||||
// Try type arg first
|
||||
if (argType === 'string' && arg in types) {
|
||||
type = arg;
|
||||
if (this._read)
|
||||
read = 1; // will be increased below
|
||||
components = slice.call(arguments, 1);
|
||||
}
|
||||
if (components) {
|
||||
if (!type)
|
||||
// type = arg.length >= 4
|
||||
// ? 'cmyk'
|
||||
// : arg.length >= 3
|
||||
type = components.length >= 3
|
||||
? 'rgb'
|
||||
: 'gray';
|
||||
var length = types[type].length;
|
||||
alpha = components[length];
|
||||
if (this._read)
|
||||
this._read = components === arguments
|
||||
read += components === arguments
|
||||
? length + (alpha != null ? 1 : 0)
|
||||
: 1;
|
||||
components = Array.prototype.slice.call(components, 0, length);
|
||||
components = slice.call(components, 0, length);
|
||||
} else {
|
||||
if (argType === 'object') {
|
||||
if (argType === 'string') {
|
||||
type = 'rgb';
|
||||
components = arg.match(/^#[0-9a-f]{3,6}$/i)
|
||||
? hexToRgb(arg)
|
||||
: nameToRgb(arg);
|
||||
} else if (argType === 'object') {
|
||||
if (arg._class === 'Color') {
|
||||
type = arg._type;
|
||||
components = arg._components.slice();
|
||||
|
@ -260,23 +275,21 @@ var Color = this.Color = Base.extend(new function() {
|
|||
? 'gray'
|
||||
: 'rgb';
|
||||
var properties = types[type];
|
||||
components = [];
|
||||
for (var i = 0, l = properties.length; i < l; i++)
|
||||
components[i] = arg[properties[i]] || 0;
|
||||
alpha = arg.alpha;
|
||||
}
|
||||
} else if (argType === 'string') {
|
||||
components = arg.match(/^#[0-9a-f]{3,6}$/i)
|
||||
? hexToRgb(arg)
|
||||
: nameToRgb(arg);
|
||||
type = 'rgb';
|
||||
}
|
||||
if (this._read && type)
|
||||
this._read = 1;
|
||||
read = 1;
|
||||
}
|
||||
// Default fallbacks: rgb, black
|
||||
this._type = type || 'rgb';
|
||||
this._components = components || (type === 'gray' ? [1] : [0, 0, 0]);
|
||||
this._alpha = alpha;
|
||||
if (this._read)
|
||||
this._read = read;
|
||||
},
|
||||
|
||||
_serialize: function(options) {
|
||||
|
|
|
@ -142,25 +142,23 @@ var Item = this.Item = Base.extend(Callback, {
|
|||
var props = {},
|
||||
that = this;
|
||||
|
||||
function serialize(fields, compact) {
|
||||
function serialize(fields) {
|
||||
for (var key in fields) {
|
||||
var value = that[key];
|
||||
if (!Base.equals(value, fields[key]))
|
||||
props[key] = Base.serialize(value, options,
|
||||
// Do not use compact mode for data
|
||||
compact && key !== 'data', dictionary);
|
||||
key !== 'data', dictionary);
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize fields that this Item subclass defines first
|
||||
serialize(this._serializeFields, true);
|
||||
serialize(this._serializeFields);
|
||||
// Serialize style fields, but only if they differ from defaults.
|
||||
// Do not use compact mode here, since colors always need their type
|
||||
// identifiers.
|
||||
// Do not serialize styles on Groups and Layers, since they just unify
|
||||
// their children's own styles.
|
||||
if (!(this instanceof Group))
|
||||
serialize(this._style._defaults, false);
|
||||
serialize(this._style._defaults);
|
||||
// There is no compact form for Item serialization, we always keep the
|
||||
// type.
|
||||
return [ this._class, props ];
|
||||
|
|
Loading…
Reference in a new issue