The Color.HSVToRGB function has been rewritten to use the HSL and HSV formula from Wikipedia, giving much better results. Fix #5089

This commit is contained in:
Richard Davey 2020-09-01 18:06:23 +01:00
parent 9a2643852e
commit 506426f706

View file

@ -7,10 +7,32 @@
var GetColor = require('./GetColor');
/**
* Converts an HSV (hue, saturation and value) color value to RGB.
* Conversion formula from http://en.wikipedia.org/wiki/HSL_color_space.
* RGB space conversion.
*
* @ignore
*
* @param {number} n - The value to convert.
* @param {number} h - The h value.
* @param {number} s - The s value.
* @param {number} v - The v value.
*
* @return {number} The converted value.
*/
function ConvertValue (n, h, s, v)
{
var k = (n + h * 6) % 6;
var min = Math.min(k, 4 - k, 1);
return Math.round(255 * (v - v * s * Math.max(0, min)));
}
/**
* Converts a HSV (hue, saturation and value) color set to RGB.
*
* Conversion formula from https://en.wikipedia.org/wiki/HSL_and_HSV
*
* Assumes HSV values are contained in the set [0, 1].
* Based on code by Michael Jackson (https://github.com/mjijackson)
*
* @function Phaser.Display.Color.HSVToRGB
* @since 3.0.0
@ -27,51 +49,9 @@ var HSVToRGB = function (h, s, v, out)
if (s === undefined) { s = 1; }
if (v === undefined) { v = 1; }
var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = Math.floor((v * (1 - s)) * 255);
var q = Math.floor((v * (1 - f * s)) * 255);
var t = Math.floor((v * (1 - (1 - f) * s)) * 255);
v = Math.floor(v *= 255);
var r = v;
var g = v;
var b = v;
var c = i % 6;
if (c === 0)
{
g = t;
b = p;
}
else if (c === 1)
{
r = q;
b = p;
}
else if (c === 2)
{
r = p;
b = t;
}
else if (c === 3)
{
r = p;
g = q;
}
else if (c === 4)
{
r = t;
g = p;
}
else if (c === 5)
{
g = p;
b = q;
}
var r = ConvertValue(5, h, s, v);
var g = ConvertValue(3, h, s, v);
var b = ConvertValue(1, h, s, v);
if (!out)
{