Merge pull request #20 from gka/hsl-color

added support for HSL colors
This commit is contained in:
Jürg Lehni 2011-07-06 14:37:19 -07:00
commit f357eb4a21
3 changed files with 753 additions and 154 deletions

753
dist/paper.js vendored

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HSLColor</title>
<link rel="stylesheet" href="../css/style.css">
<script type="text/javascript" src="../../dist/paper.js"></script>
<script type="text/paperscript" canvas="canvas">
var center = view.center;
var path,
h,
s = 0.6,
l = 0.6,
d = view.size.width * 0.012;
p = new Point(),
r = view.size.width * .12;
for (s = 0; s <= 1; s+=0.2) {
for (h = 0; h < (s > 0 ? 360 : 1); h+= 20) {
p.x = (center.x - r)*0.5 + Math.cos((h-90)/180*Math.PI) * r*s;
p.y = center.y + Math.sin((h-90)/180*Math.PI) * r*s;
path = new Path.Circle(p, Math.max(d*Math.sqrt(s), 5));
path.fillColor = new HSLColor(h, s, 0.75);
p.x = center.x + Math.cos((h-90)/180*Math.PI) * r*s;
p.y = center.y + Math.sin((h-90)/180*Math.PI) * r*s;
path = new Path.Circle(p, Math.max(d*Math.sqrt(s), 5));
path.fillColor = new HSLColor(h, s, 0.5);
p.x = center.x + (center.x + r) * .5 + Math.cos((h-90)/180*Math.PI) * r*s;
p.y = center.y + Math.sin((h-90)/180*Math.PI) * r*s;
path = new Path.Circle(p, Math.max(d*Math.sqrt(s), 5));
path.fillColor = new HSLColor(h, s, 0.25);
}
}
</script>
</head>
<body>
<canvas id="canvas" resize></canvas>
</body>
</html>

View file

@ -51,7 +51,8 @@ var Color = this.Color = Base.extend(new function() {
var components = {
gray: ['gray'],
rgb: ['red', 'green', 'blue'],
hsb: ['hue', 'saturation', 'brightness']
hsb: ['hue', 'saturation', 'brightness'],
hsl: ['hue', 'saturation', 'lightness']
};
var colorCache = {},
@ -164,6 +165,77 @@ var Color = this.Color = Base.extend(new function() {
'gray-hsb': function(color) {
return new HSBColor(0, 0, 1 - color._gray, color._alpha);
},
'rgb-hsl': function(color) {
// Code taken from
// http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
var r = color._red,
g = color._green,
b = color._blue,
max = Math.max(r, g, b),
min = Math.min(r, g, b),
h, s, l = (max + min) / 2;
if (max == min) {
h = s = 0;
} else {
s = l < 0.5 ? (max - min) / (max + min) : (max - min) / (2 - max - min);
}
switch (max) {
case r: h = (g - b) / (max - min); break;
case g: h = 2 + (b - r) / (max - min); break;
case b: h = 4 + (r - g) / (max - min); break;
}
h *= 60;
if (h < 0) h += 360;
return new HSLColor(h, s, l, color._alpha);
},
'hsl-rgb': function(color) {
// this code is a slightly modified version of this source:
// http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
var s = color._saturation,
h = color._hue / 360,
l = color._lightness,
t1, t2, t3, c, r, g, b, i;
if (s == 0) {
return new RGBColor(l, l, l, color._alpha);
} else {
t3 = [0,0,0];
c = [0,0,0];
t2 = l < 0.5 ? l * (1 + s) : l + s - l * s;
t1 = 2 * l - t2;
t3[0] = h + 1 / 3;
t3[1] = h;
t3[2] = h - 1 / 3;
for (i = 0; i<3; i++) {
if (t3[i] < 0) t3[i] += 1;
if (t3[i] > 1) t3[i] -= 1;
if (6 * t3[i] < 1) c[i] = t1 + (t2 - t1) * 6 * t3[i];
else if (2 * t3[i] < 1) c[i] = t2;
else if (3 * t3[i] < 2) c[i] = t1 + (t2 - t1) * ((2 / 3) - t3[i]) * 6;
else c[i] = t1;
}
return new RGBColor(c[0], c[1], c[2], color._alpha);
}
},
'hsl-gray': function(color) {
return converters['rgb-gray'](converters['hsl-rgb'](color));
},
'gray-hsl': function(color) {
return new HSLColor(0, 0, 1 - color._gray, color._alpha);
},
'hsl-hsb': function(color) {
return converters['rgb-hsb'](converters['hsl-rgb'](color));
},
'hsb-hsl': function(color) {
return converters['rgb-hsl'](converters['hsb-rgb'](color));
}
};
@ -181,6 +253,9 @@ var Color = this.Color = Base.extend(new function() {
? new RGBColor(arg.red, arg.green, arg.blue, arg.alpha)
: arg.gray !== undefined
? new GrayColor(arg.gray, arg.alpha)
: arg.lightness !== undefined
? new HSLColor(arg.hue, arg.saturation, arg.lightness,
arg.alpha)
: arg.hue !== undefined
? new HSBColor(arg.hue, arg.saturation, arg.brightness,
arg.alpha)
@ -670,3 +745,38 @@ var HSBColor = this.HSBColor = Color.extend(/** @lends HSBColor# */{
_colorType: 'hsb'
});
/**
* @name HSLColor
* @class An HSLColor object is used to represent any HSL color value.
* @extends Color
*/
var HSLColor = this.HSLColor = Color.extend(/** @lends HSLColor# */{
/**
* Creates an HSLColor object
*
* @name HSLColor#initialize
* @param {Number} hue the hue of the color as a value in degrees between
* {@code 0} and {@code 360}.
* @param {Number} saturation the saturation of the color as a value
* between {@code 0} and {@code 1}
* @param {Number} lightness the lightness of the color as a value
* between {@code 0} and {@code 1}
* @param {Number} [alpha] the alpha of the color as a value between
* {@code 0} and {@code 1}
*
* @example {@paperscript}
* // Creating an HSLColor:
*
* // Create a circle shaped path at {x: 80, y: 50}
* // with a radius of 30:
* var circle = new Path.Circle(new Point(80, 50), 30);
*
* // Create an HSLColor with a hue of 90 degrees, a saturation
* // 100% and a lightness of 100%:
* circle.fillColor = new HSLColor(90, 1, 1);
*/
_colorType: 'hsl'
});