mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-19 14:10:14 -05:00
Implement Color component clamping for color conversion.
This commit is contained in:
parent
42bed58624
commit
9dea3f3b74
1 changed files with 20 additions and 2 deletions
|
@ -647,6 +647,24 @@ var Color = Base.extend(new function() {
|
|||
this._owner._changed(/*#=*/ Change.STYLE);
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns a copy of the components array with all values clamped to
|
||||
* valid numbers, based on the type of property they represent.
|
||||
*/
|
||||
_clamp: function() {
|
||||
var components = this._components.slice(),
|
||||
properties = this._properties;
|
||||
if (this._type !== 'gradient') {
|
||||
for (var i = 0, l = properties.length; i < l; i++) {
|
||||
var value = components[i];
|
||||
components[i] = properties[i] === 'hue'
|
||||
? ((value % 360) + 360) % 360
|
||||
: value < 0 ? 0 : value > 1 ? 1 : value;
|
||||
}
|
||||
}
|
||||
return components;
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {Number[]} the converted components as an array
|
||||
*/
|
||||
|
@ -655,11 +673,11 @@ var Color = Base.extend(new function() {
|
|||
return this._type === type
|
||||
? this._components.slice()
|
||||
: (converter = converters[this._type + '-' + type])
|
||||
? converter.apply(this, this._components)
|
||||
? converter.apply(this, this._clamp())
|
||||
// Convert to and from rgb if no direct converter exists
|
||||
: converters['rgb-' + type].apply(this,
|
||||
converters[this._type + '-rgb'].apply(this,
|
||||
this._components));
|
||||
this._clamp()));
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue