Skip to content

Commit 0492e9e

Browse files
author
Lauren McCarthy
authored
Merge pull request #1460 from joecridge/hsb-with-max-hue
Rearrange if statement in _hsbaToRGBA, fixes #1459
2 parents 3ed6fab + e770b36 commit 0492e9e

File tree

2 files changed

+59
-37
lines changed

2 files changed

+59
-37
lines changed

src/color/color_conversion.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,7 @@ p5.ColorConversion._hsbaToRGBA = function(hsba) {
6161
var tint2 = val * (1 - sat * (hue - sector));
6262
var tint3 = val * (1 - sat * (1 + sector - hue));
6363
var red, green, blue;
64-
if (sector === 0) { // Red to yellow.
65-
red = val;
66-
green = tint3;
67-
blue = tint1;
68-
} else if (sector === 1) { // Yellow to green.
64+
if (sector === 1) { // Yellow to green.
6965
red = tint2;
7066
green = val;
7167
blue = tint1;
@@ -81,10 +77,14 @@ p5.ColorConversion._hsbaToRGBA = function(hsba) {
8177
red = tint3;
8278
green = tint1;
8379
blue = val;
84-
} else { // Magenta to red.
80+
} else if (sector === 5) { // Magenta to red.
8581
red = val;
8682
green = tint1;
8783
blue = tint2;
84+
} else { // Red to yellow (sector could be 0 or 6).
85+
red = val;
86+
green = tint3;
87+
blue = tint1;
8888
}
8989
RGBA = [red, green, blue, hsba[3]];
9090
}

test/unit/color/color_conversion.js

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,99 @@
11
suite('p5.ColorConversion', function() {
22
var rgba = [1, 0, 0.4, 0.8];
33
var hsla = [336/360, 1, 0.5, 0.8];
4+
var hslaWithMaxHue = [1, 1, 0.5, 0.6];
45
var hsba = [336/360, 1, 1, 0.8];
5-
var one;
6+
var hsbaWithMaxHue = [1, 1, 1, 0.6];
7+
var result;
68

79
suite('rgbaToHSBA', function() {
810
test('rgba converts to hsba', function() {
9-
one = p5.ColorConversion._rgbaToHSBA(rgba);
11+
result = p5.ColorConversion._rgbaToHSBA(rgba);
1012
assert.deepEqual([
11-
Math.round(one[0] * 360),
12-
Math.round(one[1] * 100),
13-
Math.round(one[2] * 100),
14-
one[3]
13+
Math.round(result[0] * 360),
14+
Math.round(result[1] * 100),
15+
Math.round(result[2] * 100),
16+
result[3]
1517
], [336, 100, 100, 0.8]);
1618
});
1719
});
1820

1921
suite('hsbaToRGBA', function() {
2022
test('hsba converts to rgba', function() {
21-
one = p5.ColorConversion._hsbaToRGBA(hsba);
23+
result = p5.ColorConversion._hsbaToRGBA(hsba);
2224
assert.deepEqual([
23-
Math.round(one[0] * 255),
24-
Math.round(one[1] * 255),
25-
Math.round(one[2] * 255),
26-
Math.round(one[3] * 255)
25+
Math.round(result[0] * 255),
26+
Math.round(result[1] * 255),
27+
Math.round(result[2] * 255),
28+
Math.round(result[3] * 255)
2729
], [255, 0, 102, 204]);
2830
});
31+
32+
test('handles maximum hue value', function() {
33+
result = p5.ColorConversion._hsbaToRGBA(hsbaWithMaxHue);
34+
assert.deepEqual([
35+
Math.round(result[0] * 255),
36+
Math.round(result[1] * 255),
37+
Math.round(result[2] * 255),
38+
Math.round(result[3] * 255)
39+
], [255, 0, 0, 153]);
40+
});
2941
});
3042

3143
suite('hslaToRGBA', function() {
3244
test('hsla converts to rgba', function() {
33-
one = p5.ColorConversion._hslaToRGBA(hsla);
45+
result = p5.ColorConversion._hslaToRGBA(hsla);
3446
assert.deepEqual([
35-
Math.round(one[0] * 255),
36-
Math.round(one[1] * 255),
37-
Math.round(one[2] * 255),
38-
Math.round(one[3] * 255)
47+
Math.round(result[0] * 255),
48+
Math.round(result[1] * 255),
49+
Math.round(result[2] * 255),
50+
Math.round(result[3] * 255)
3951
], [255, 0, 102, 204]);
4052
});
53+
54+
test('handles maximum hue value', function() {
55+
result = p5.ColorConversion._hslaToRGBA(hslaWithMaxHue);
56+
assert.deepEqual([
57+
Math.round(result[0] * 255),
58+
Math.round(result[1] * 255),
59+
Math.round(result[2] * 255),
60+
Math.round(result[3] * 255)
61+
], [255, 0, 0, 153]);
62+
});
4163
});
4264

4365
suite('rgbaToHSLA', function() {
4466
test('rgba converts to hsla', function() {
45-
one = p5.ColorConversion._rgbaToHSLA(rgba);
67+
result = p5.ColorConversion._rgbaToHSLA(rgba);
4668
assert.deepEqual([
47-
Math.round(one[0] * 360),
48-
Math.round(one[1] * 100),
49-
Math.round(one[2] * 100),
50-
one[3]
69+
Math.round(result[0] * 360),
70+
Math.round(result[1] * 100),
71+
Math.round(result[2] * 100),
72+
result[3]
5173
], [336, 100, 50, 0.8]);
5274
});
5375
});
5476

5577
suite('hslaToHSBA', function() {
5678
test('hsla converts to hsba', function() {
57-
one = p5.ColorConversion._hslaToHSBA(hsla);
79+
result = p5.ColorConversion._hslaToHSBA(hsla);
5880
assert.deepEqual([
59-
Math.round(one[0] * 360),
60-
Math.round(one[1] * 100),
61-
Math.round(one[2] * 100),
62-
one[3]
81+
Math.round(result[0] * 360),
82+
Math.round(result[1] * 100),
83+
Math.round(result[2] * 100),
84+
result[3]
6385
], [336, 100, 100, 0.8]);
6486
});
6587
});
6688

6789
suite('hsbaToHSLA', function() {
6890
test('hsba converts to hsla', function() {
69-
one = p5.ColorConversion._hsbaToHSLA(hsba);
91+
result = p5.ColorConversion._hsbaToHSLA(hsba);
7092
assert.deepEqual([
71-
Math.round(one[0] * 360),
72-
Math.round(one[1] * 100),
73-
Math.round(one[2] * 100),
74-
one[3]
93+
Math.round(result[0] * 360),
94+
Math.round(result[1] * 100),
95+
Math.round(result[2] * 100),
96+
result[3]
7597
], [336, 100, 50, 0.8]);
7698
});
7799
});

0 commit comments

Comments
 (0)