2011-03-06 19:50:44 -05:00
|
|
|
/*
|
|
|
|
* Paper.js
|
|
|
|
*
|
|
|
|
* This file is part of Paper.js, a JavaScript Vector Graphics Library,
|
|
|
|
* based on Scriptographer.org and designed to be largely API compatible.
|
2011-03-07 20:41:50 -05:00
|
|
|
* http://paperjs.org/
|
2011-03-06 19:50:44 -05:00
|
|
|
* http://scriptographer.org/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* Distributed under the MIT license. See LICENSE file for details.
|
|
|
|
*
|
2011-03-06 19:50:44 -05:00
|
|
|
* Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
|
|
|
|
* http://lehni.org/ & http://jonathanpuckey.com/
|
|
|
|
*
|
2011-03-07 20:41:50 -05:00
|
|
|
* All rights reserved.
|
2011-03-06 19:50:44 -05:00
|
|
|
*/
|
|
|
|
|
2011-03-04 08:34:31 -05:00
|
|
|
var Color = this.Color = Base.extend({
|
2011-02-19 11:10:26 -05:00
|
|
|
beans: true,
|
|
|
|
|
2011-03-04 08:19:07 -05:00
|
|
|
initialize: function() {
|
|
|
|
var rgb = new RGBColor(RGBColor.dont);
|
|
|
|
rgb.initialize.apply(rgb, arguments);
|
|
|
|
return rgb;
|
|
|
|
},
|
|
|
|
|
2011-02-19 11:10:26 -05:00
|
|
|
/**
|
|
|
|
* A value between 0 and 1 that specifies the color's alpha value.
|
|
|
|
* All colors of the different subclasses support alpha values.
|
|
|
|
*/
|
|
|
|
getAlpha: function() {
|
2011-03-04 19:16:26 -05:00
|
|
|
return this._alpha != null ? this._alpha : 1;
|
2011-02-19 11:10:26 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-19 11:10:26 -05:00
|
|
|
setAlpha: function(alpha) {
|
2011-03-07 11:51:00 -05:00
|
|
|
this._alpha = Math.min(Math.max(alpha, 0), 1);
|
|
|
|
this._alpha = alpha < 0 ? 0 : alpha > 1 ? 1 : alpha;
|
2011-02-19 12:01:08 -05:00
|
|
|
this._cssString = null;
|
2011-02-19 11:10:26 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-19 11:10:26 -05:00
|
|
|
/**
|
|
|
|
* Checks if the color has an alpha value.
|
2011-03-07 09:59:35 -05:00
|
|
|
*
|
2011-02-19 11:10:26 -05:00
|
|
|
* @return {@true if the color has an alpha value}
|
|
|
|
*/
|
|
|
|
hasAlpha: function() {
|
2011-03-04 19:16:26 -05:00
|
|
|
return this._alpha != null;
|
2011-02-19 11:10:26 -05:00
|
|
|
},
|
2011-03-03 17:45:17 -05:00
|
|
|
|
2011-02-19 16:50:37 -05:00
|
|
|
getCanvasStyle: function() {
|
2011-03-04 12:14:24 -05:00
|
|
|
return this.toCssString();
|
2011-02-19 11:10:26 -05:00
|
|
|
}
|
2011-03-07 09:59:35 -05:00
|
|
|
}, new function() {
|
|
|
|
function colorToHsb(color) {
|
2011-03-07 11:51:00 -05:00
|
|
|
var r = color.getRed(),
|
|
|
|
g = color.getGreen(),
|
|
|
|
b = color.getBlue(),
|
2011-03-07 13:36:13 -05:00
|
|
|
max = Math.max(r, g, b),
|
2011-03-07 11:51:00 -05:00
|
|
|
min = Math.min(r, g, b),
|
|
|
|
delta = max - min,
|
|
|
|
hue,
|
|
|
|
saturation = (max != 0) ? delta / max : 0,
|
2011-03-07 09:59:35 -05:00
|
|
|
brightness = max;
|
|
|
|
if (saturation == 0) {
|
|
|
|
hue = 0;
|
|
|
|
} else {
|
|
|
|
var rr = (max - r) / delta,
|
|
|
|
gr = (max - g) / delta,
|
|
|
|
br = (max - b) / delta;
|
2011-03-07 11:51:00 -05:00
|
|
|
hue = r == max
|
|
|
|
? br - gr
|
|
|
|
: g == max
|
|
|
|
? 2 + rr - br
|
|
|
|
: 4 + gr - rr;
|
2011-03-07 09:59:35 -05:00
|
|
|
hue /= 6;
|
2011-03-07 11:51:00 -05:00
|
|
|
if (hue < 0)
|
|
|
|
hue++;
|
2011-03-07 09:59:35 -05:00
|
|
|
}
|
2011-03-07 11:51:00 -05:00
|
|
|
return [hue * 360, saturation, brightness];
|
2011-03-07 09:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function hsbToRgb(hue, saturation, brightness) {
|
2011-03-07 11:51:00 -05:00
|
|
|
if (hue < 0)
|
|
|
|
hue += 360;
|
2011-03-07 09:59:35 -05:00
|
|
|
hue = hue % 360;
|
2011-03-07 11:51:00 -05:00
|
|
|
var f = hue % 60,
|
|
|
|
p = (brightness * (1 - saturation)) / 1,
|
2011-03-07 09:59:35 -05:00
|
|
|
q = (brightness * (60 - saturation * f)) / 60,
|
|
|
|
t = (brightness * (60 - saturation * (60 - f))) / 60;
|
|
|
|
switch (Math.floor(hue / 60)) {
|
|
|
|
case 0: return [brightness, t, p];
|
|
|
|
case 1: return [q, brightness, p];
|
|
|
|
case 2: return [p, brightness, t];
|
|
|
|
case 3: return [p, q, brightness];
|
|
|
|
case 4: return [t, p, brightness];
|
|
|
|
case 5: return [brightness, p, q];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Base.each(
|
|
|
|
['hue', 'saturation', 'brightness'],
|
|
|
|
function(key, index) {
|
|
|
|
this['get' + Base.capitalize(key)] = function() {
|
|
|
|
return colorToHsb(this)[index];
|
|
|
|
};
|
|
|
|
this['set' + Base.capitalize(key)] = function(value) {
|
|
|
|
var hsb = colorToHsb(this);
|
|
|
|
hsb[index] = value;
|
|
|
|
var rgb = hsbToRgb.apply(this, hsb);
|
|
|
|
this.setRed(rgb[0]).setGreen(rgb[1]).setBlue(rgb[2]);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
{ beans: true }
|
|
|
|
);
|
|
|
|
});
|