Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/gui/common/color.js
Show All 26 Lines | |||||
* True if the colors are identical. | * True if the colors are identical. | ||||
* | * | ||||
* @param {Object} color1 | * @param {Object} color1 | ||||
* @param {Object} color2 | * @param {Object} color2 | ||||
* @returns {boolean} | * @returns {boolean} | ||||
*/ | */ | ||||
function sameColor(color1, color2) | function sameColor(color1, color2) | ||||
{ | { | ||||
return color1.r === color2.r && color1.g === color2.g && color1.b === color2.b; | return color1.r === color2.r && color1.g === color2.g && color1.b === color2.b; | ||||
} | } | ||||
/** | /** | ||||
* Computes the euclidian distance between the two colors. | * Computes the euclidian distance between the two colors. | ||||
* The smaller the return value, the close the colors. Zero if identical. | * The smaller the return value, the close the colors. Zero if identical. | ||||
* | * | ||||
* @param {Object} color1 | * @param {Object} color1 | ||||
* @param {Object} color2 | * @param {Object} color2 | ||||
Show All 35 Lines | function rgbToHsl(r, g, b) | ||||
if (max == min) | if (max == min) | ||||
h = s = 0; // achromatic | h = s = 0; // achromatic | ||||
else | else | ||||
{ | { | ||||
let d = max - min; | let d = max - min; | ||||
s = l > 0.5 ? d / (2 - max - min) : d / (max + min); | s = l > 0.5 ? d / (2 - max - min) : d / (max + min); | ||||
switch (max) | switch (max) | ||||
{ | { | ||||
case r: h = (g - b) / d + (g < b ? 6 : 0); break; | case r: | ||||
case g: h = (b - r) / d + 2; break; | h = (g - b) / d + (g < b ? 6 : 0); | ||||
case b: h = (r - g) / d + 4; break; | break; | ||||
case g: | |||||
h = (b - r) / d + 2; | |||||
break; | |||||
case b: | |||||
h = (r - g) / d + 4; | |||||
break; | |||||
} | } | ||||
h /= 6; | h /= 6; | ||||
} | } | ||||
return [h, s, l]; | return [h, s, l]; | ||||
} | } | ||||
/** | /** | ||||
* Convert color value from HSL to RGB space. | * Convert color value from HSL to RGB space. | ||||
* | * | ||||
* @see {@link http://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion} | * @see {@link http://stackoverflow.com/questions/2353211/hsl-to-rgb-color-conversion} | ||||
* @param {number} h - hueness | * @param {number} h - hueness | ||||
* @param {number} s - saturation | * @param {number} s - saturation | ||||
* @param {number} l - lightness | * @param {number} l - lightness | ||||
* @returns {Array} | * @returns {Array} | ||||
*/ | */ | ||||
function hslToRgb(h, s, l) | function hslToRgb(h, s, l) | ||||
{ | { | ||||
function hue2rgb(p, q, t) | function hue2rgb(p, q, t) | ||||
{ | { | ||||
if (t < 0) t += 1; | if (t < 0) | ||||
if (t > 1) t -= 1; | t += 1; | ||||
if (t < 1/6) return p + (q - p) * 6 * t; | if (t > 1) | ||||
if (t < 1/2) return q; | t -= 1; | ||||
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6; | if (t < 1/6) | ||||
return p + (q - p) * 6 * t; | |||||
if (t < 1/2) | |||||
return q; | |||||
if (t < 2/3) | |||||
return p + (q - p) * (2/3 - t) * 6; | |||||
return p; | return p; | ||||
} | } | ||||
[h, s, l] = [h, s, l].map(clampColorValue); | [h, s, l] = [h, s, l].map(clampColorValue); | ||||
let r, g, b; | let r, g, b; | ||||
if (s == 0) | if (s == 0) | ||||
r = g = b = l; // achromatic | r = g = b = l; // achromatic | ||||
else { | else { | ||||
▲ Show 20 Lines • Show All 46 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator