Fix issues with new Color serialization.

This commit is contained in:
Jürg Lehni 2013-04-08 13:10:49 -07:00
parent cf21da75fc
commit 01673d675d
2 changed files with 37 additions and 26 deletions

View file

@ -219,30 +219,45 @@ var Color = this.Color = Base.extend(new function() {
initialize: function(arg) { initialize: function(arg) {
// We are storing color internally as an array of components // We are storing color internally as an array of components
var argType = arg != null && typeof arg, var slice = Array.prototype.slice,
type, argType = arg != null && typeof arg,
components = argType === 'number' components = argType === 'number'
? arguments ? arguments
: Array.isArray(arg) : Array.isArray(arg)
? arg ? arg
: [], : null,
read = 0,
type,
alpha; alpha;
if (components.length > 0) { // Try type arg first
// type = arg.length >= 4 if (argType === 'string' && arg in types) {
// ? 'cmyk' type = arg;
// : arg.length >= 3 if (this._read)
type = components.length >= 3 read = 1; // will be increased below
? 'rgb' components = slice.call(arguments, 1);
: 'gray'; }
if (components) {
if (!type)
// type = arg.length >= 4
// ? 'cmyk'
// : arg.length >= 3
type = components.length >= 3
? 'rgb'
: 'gray';
var length = types[type].length; var length = types[type].length;
alpha = components[length]; alpha = components[length];
if (this._read) if (this._read)
this._read = components === arguments read += components === arguments
? length + (alpha != null ? 1 : 0) ? length + (alpha != null ? 1 : 0)
: 1; : 1;
components = Array.prototype.slice.call(components, 0, length); components = slice.call(components, 0, length);
} else { } 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') { if (arg._class === 'Color') {
type = arg._type; type = arg._type;
components = arg._components.slice(); components = arg._components.slice();
@ -260,23 +275,21 @@ var Color = this.Color = Base.extend(new function() {
? 'gray' ? 'gray'
: 'rgb'; : 'rgb';
var properties = types[type]; var properties = types[type];
components = [];
for (var i = 0, l = properties.length; i < l; i++) for (var i = 0, l = properties.length; i < l; i++)
components[i] = arg[properties[i]] || 0; components[i] = arg[properties[i]] || 0;
alpha = arg.alpha; 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) if (this._read && type)
this._read = 1; read = 1;
} }
// Default fallbacks: rgb, black // Default fallbacks: rgb, black
this._type = type || 'rgb'; this._type = type || 'rgb';
this._components = components || (type === 'gray' ? [1] : [0, 0, 0]); this._components = components || (type === 'gray' ? [1] : [0, 0, 0]);
this._alpha = alpha; this._alpha = alpha;
if (this._read)
this._read = read;
}, },
_serialize: function(options) { _serialize: function(options) {

View file

@ -142,25 +142,23 @@ var Item = this.Item = Base.extend(Callback, {
var props = {}, var props = {},
that = this; that = this;
function serialize(fields, compact) { function serialize(fields) {
for (var key in fields) { for (var key in fields) {
var value = that[key]; var value = that[key];
if (!Base.equals(value, fields[key])) if (!Base.equals(value, fields[key]))
props[key] = Base.serialize(value, options, props[key] = Base.serialize(value, options,
// Do not use compact mode for data // Do not use compact mode for data
compact && key !== 'data', dictionary); key !== 'data', dictionary);
} }
} }
// Serialize fields that this Item subclass defines first // 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. // 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 // Do not serialize styles on Groups and Layers, since they just unify
// their children's own styles. // their children's own styles.
if (!(this instanceof Group)) 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 // There is no compact form for Item serialization, we always keep the
// type. // type.
return [ this._class, props ]; return [ this._class, props ];