From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- toolkit/components/places/ColorConversion.js | 64 ++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 toolkit/components/places/ColorConversion.js (limited to 'toolkit/components/places/ColorConversion.js') diff --git a/toolkit/components/places/ColorConversion.js b/toolkit/components/places/ColorConversion.js new file mode 100644 index 000000000..b8a2c860d --- /dev/null +++ b/toolkit/components/places/ColorConversion.js @@ -0,0 +1,64 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * Given a color in the Lab space, return its chroma (colorfulness, + * saturation). + * + * @param lab + * The lab color to get the chroma from + * + * @return A number greater than zero that measures chroma in the image + */ +function labChroma(lab) { + return Math.sqrt(Math.pow(lab.a, 2) + Math.pow(lab.b, 2)); +} + +/** + * Given the RGB components of a color as integers from 0-255, return the + * color in the XYZ color space. + * + * @return An object with x, y, z properties holding those components of the + * color in the XYZ color space. + */ +function rgb2xyz(r, g, b) { + r /= 255; + g /= 255; + b /= 255; + + // assume sRGB + r = r > 0.04045 ? Math.pow(((r + 0.055) / 1.055), 2.4) : (r / 12.92); + g = g > 0.04045 ? Math.pow(((g + 0.055) / 1.055), 2.4) : (g / 12.92); + b = b > 0.04045 ? Math.pow(((b + 0.055) / 1.055), 2.4) : (b / 12.92); + + return { + x: ((r * 0.4124) + (g * 0.3576) + (b * 0.1805)) * 100, + y: ((r * 0.2126) + (g * 0.7152) + (b * 0.0722)) * 100, + z: ((r * 0.0193) + (g * 0.1192) + (b * 0.9505)) * 100 + }; +} + +/** + * Given the RGB components of a color as integers from 0-255, return the + * color in the Lab color space. + * + * @return An object with lightness, a, b properties holding those components + * of the color in the Lab color space. + */ +function rgb2lab(r, g, b) { + let xyz = rgb2xyz(r, g, b), + x = xyz.x / 95.047, + y = xyz.y / 100, + z = xyz.z / 108.883; + + x = x > 0.008856 ? Math.pow(x, 1/3) : (7.787 * x) + (16 / 116); + y = y > 0.008856 ? Math.pow(y, 1/3) : (7.787 * y) + (16 / 116); + z = z > 0.008856 ? Math.pow(z, 1/3) : (7.787 * z) + (16 / 116); + + return { + lightness: (116 * y) - 16, + a: 500 * (x - y), + b: 200 * (y - z) + }; +} -- cgit v1.2.3