michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Given a color in the Lab space, return its chroma (colorfulness, michael@0: * saturation). michael@0: * michael@0: * @param lab michael@0: * The lab color to get the chroma from michael@0: * michael@0: * @return A number greater than zero that measures chroma in the image michael@0: */ michael@0: function labChroma(lab) { michael@0: return Math.sqrt(Math.pow(lab.a, 2) + Math.pow(lab.b, 2)); michael@0: } michael@0: michael@0: /** michael@0: * Given the RGB components of a color as integers from 0-255, return the michael@0: * color in the XYZ color space. michael@0: * michael@0: * @return An object with x, y, z properties holding those components of the michael@0: * color in the XYZ color space. michael@0: */ michael@0: function rgb2xyz(r, g, b) { michael@0: r /= 255; michael@0: g /= 255; michael@0: b /= 255; michael@0: michael@0: // assume sRGB michael@0: r = r > 0.04045 ? Math.pow(((r + 0.055) / 1.055), 2.4) : (r / 12.92); michael@0: g = g > 0.04045 ? Math.pow(((g + 0.055) / 1.055), 2.4) : (g / 12.92); michael@0: b = b > 0.04045 ? Math.pow(((b + 0.055) / 1.055), 2.4) : (b / 12.92); michael@0: michael@0: return { michael@0: x: ((r * 0.4124) + (g * 0.3576) + (b * 0.1805)) * 100, michael@0: y: ((r * 0.2126) + (g * 0.7152) + (b * 0.0722)) * 100, michael@0: z: ((r * 0.0193) + (g * 0.1192) + (b * 0.9505)) * 100 michael@0: }; michael@0: } michael@0: michael@0: /** michael@0: * Given the RGB components of a color as integers from 0-255, return the michael@0: * color in the Lab color space. michael@0: * michael@0: * @return An object with lightness, a, b properties holding those components michael@0: * of the color in the Lab color space. michael@0: */ michael@0: function rgb2lab(r, g, b) { michael@0: let xyz = rgb2xyz(r, g, b), michael@0: x = xyz.x / 95.047, michael@0: y = xyz.y / 100, michael@0: z = xyz.z / 108.883; michael@0: michael@0: x = x > 0.008856 ? Math.pow(x, 1/3) : (7.787 * x) + (16 / 116); michael@0: y = y > 0.008856 ? Math.pow(y, 1/3) : (7.787 * y) + (16 / 116); michael@0: z = z > 0.008856 ? Math.pow(z, 1/3) : (7.787 * z) + (16 / 116); michael@0: michael@0: return { michael@0: lightness: (116 * y) - 16, michael@0: a: 500 * (x - y), michael@0: b: 200 * (y - z) michael@0: }; michael@0: }