mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-22 07:19:57 -05:00
Simplify HSB code a little.
This commit is contained in:
parent
350fef00ec
commit
2a0c393b85
1 changed files with 22 additions and 13 deletions
|
@ -29,9 +29,8 @@ var Color = this.Color = Base.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
setAlpha: function(alpha) {
|
setAlpha: function(alpha) {
|
||||||
if (alpha < 0) this._alpha = 0;
|
this._alpha = Math.min(Math.max(alpha, 0), 1);
|
||||||
else if (alpha > 1) this._alpha = 1;
|
this._alpha = alpha < 0 ? 0 : alpha > 1 ? 1 : alpha;
|
||||||
else this._alpha = alpha;
|
|
||||||
this._cssString = null;
|
this._cssString = null;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -49,9 +48,14 @@ var Color = this.Color = Base.extend({
|
||||||
}
|
}
|
||||||
}, new function() {
|
}, new function() {
|
||||||
function colorToHsb(color) {
|
function colorToHsb(color) {
|
||||||
var r = color.getRed(), g = color.getGreen(), b = color.getBlue(),
|
var r = color.getRed(),
|
||||||
max = Math.max(r, g, b), min = Math.min(r, g, b),
|
g = color.getGreen(),
|
||||||
delta = max - min, hue, saturation = (max != 0) ? delta / max : 0,
|
b = color.getBlue(),
|
||||||
|
max = Math.max(r, g, b)
|
||||||
|
min = Math.min(r, g, b),
|
||||||
|
delta = max - min,
|
||||||
|
hue,
|
||||||
|
saturation = (max != 0) ? delta / max : 0,
|
||||||
brightness = max;
|
brightness = max;
|
||||||
if (saturation == 0) {
|
if (saturation == 0) {
|
||||||
hue = 0;
|
hue = 0;
|
||||||
|
@ -59,19 +63,24 @@ var Color = this.Color = Base.extend({
|
||||||
var rr = (max - r) / delta,
|
var rr = (max - r) / delta,
|
||||||
gr = (max - g) / delta,
|
gr = (max - g) / delta,
|
||||||
br = (max - b) / delta;
|
br = (max - b) / delta;
|
||||||
if (r == max) hue = br - gr;
|
hue = r == max
|
||||||
else if (g == max) hue = 2 + rr - br;
|
? br - gr
|
||||||
else hue = 4 + gr - rr;
|
: g == max
|
||||||
|
? 2 + rr - br
|
||||||
|
: 4 + gr - rr;
|
||||||
hue /= 6;
|
hue /= 6;
|
||||||
if (hue < 0) hue++;
|
if (hue < 0)
|
||||||
|
hue++;
|
||||||
}
|
}
|
||||||
return [hue * 360, saturation, brightness ];
|
return [hue * 360, saturation, brightness];
|
||||||
}
|
}
|
||||||
|
|
||||||
function hsbToRgb(hue, saturation, brightness) {
|
function hsbToRgb(hue, saturation, brightness) {
|
||||||
if(hue < 0) hue += 360;
|
if (hue < 0)
|
||||||
|
hue += 360;
|
||||||
hue = hue % 360;
|
hue = hue % 360;
|
||||||
var f = hue % 60, p = (brightness * (1 - saturation)) / 1,
|
var f = hue % 60,
|
||||||
|
p = (brightness * (1 - saturation)) / 1,
|
||||||
q = (brightness * (60 - saturation * f)) / 60,
|
q = (brightness * (60 - saturation * f)) / 60,
|
||||||
t = (brightness * (60 - saturation * (60 - f))) / 60;
|
t = (brightness * (60 - saturation * (60 - f))) / 60;
|
||||||
switch (Math.floor(hue / 60)) {
|
switch (Math.floor(hue / 60)) {
|
||||||
|
|
Loading…
Reference in a new issue