Skip to content

Commit 506426f

Browse files
committed
The Color.HSVToRGB function has been rewritten to use the HSL and HSV formula from Wikipedia, giving much better results. Fix phaserjs#5089
1 parent 9a26438 commit 506426f

1 file changed

Lines changed: 28 additions & 48 deletions

File tree

src/display/color/HSVToRGB.js

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,32 @@
77
var GetColor = require('./GetColor');
88

99
/**
10-
* Converts an HSV (hue, saturation and value) color value to RGB.
11-
* Conversion formula from http://en.wikipedia.org/wiki/HSL_color_space.
10+
* RGB space conversion.
11+
*
12+
* @ignore
13+
*
14+
* @param {number} n - The value to convert.
15+
* @param {number} h - The h value.
16+
* @param {number} s - The s value.
17+
* @param {number} v - The v value.
18+
*
19+
* @return {number} The converted value.
20+
*/
21+
function ConvertValue (n, h, s, v)
22+
{
23+
var k = (n + h * 6) % 6;
24+
25+
var min = Math.min(k, 4 - k, 1);
26+
27+
return Math.round(255 * (v - v * s * Math.max(0, min)));
28+
}
29+
30+
/**
31+
* Converts a HSV (hue, saturation and value) color set to RGB.
32+
*
33+
* Conversion formula from https://en.wikipedia.org/wiki/HSL_and_HSV
34+
*
1235
* Assumes HSV values are contained in the set [0, 1].
13-
* Based on code by Michael Jackson (https://github.com/mjijackson)
1436
*
1537
* @function Phaser.Display.Color.HSVToRGB
1638
* @since 3.0.0
@@ -27,51 +49,9 @@ var HSVToRGB = function (h, s, v, out)
2749
if (s === undefined) { s = 1; }
2850
if (v === undefined) { v = 1; }
2951

30-
var i = Math.floor(h * 6);
31-
var f = h * 6 - i;
32-
33-
var p = Math.floor((v * (1 - s)) * 255);
34-
var q = Math.floor((v * (1 - f * s)) * 255);
35-
var t = Math.floor((v * (1 - (1 - f) * s)) * 255);
36-
37-
v = Math.floor(v *= 255);
38-
39-
var r = v;
40-
var g = v;
41-
var b = v;
42-
43-
var c = i % 6;
44-
45-
if (c === 0)
46-
{
47-
g = t;
48-
b = p;
49-
}
50-
else if (c === 1)
51-
{
52-
r = q;
53-
b = p;
54-
}
55-
else if (c === 2)
56-
{
57-
r = p;
58-
b = t;
59-
}
60-
else if (c === 3)
61-
{
62-
r = p;
63-
g = q;
64-
}
65-
else if (c === 4)
66-
{
67-
r = t;
68-
g = p;
69-
}
70-
else if (c === 5)
71-
{
72-
g = p;
73-
b = q;
74-
}
52+
var r = ConvertValue(5, h, s, v);
53+
var g = ConvertValue(3, h, s, v);
54+
var b = ConvertValue(1, h, s, v);
7555

7656
if (!out)
7757
{

0 commit comments

Comments
 (0)