toolkit/components/places/ColorConversion.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /**
michael@0 6 * Given a color in the Lab space, return its chroma (colorfulness,
michael@0 7 * saturation).
michael@0 8 *
michael@0 9 * @param lab
michael@0 10 * The lab color to get the chroma from
michael@0 11 *
michael@0 12 * @return A number greater than zero that measures chroma in the image
michael@0 13 */
michael@0 14 function labChroma(lab) {
michael@0 15 return Math.sqrt(Math.pow(lab.a, 2) + Math.pow(lab.b, 2));
michael@0 16 }
michael@0 17
michael@0 18 /**
michael@0 19 * Given the RGB components of a color as integers from 0-255, return the
michael@0 20 * color in the XYZ color space.
michael@0 21 *
michael@0 22 * @return An object with x, y, z properties holding those components of the
michael@0 23 * color in the XYZ color space.
michael@0 24 */
michael@0 25 function rgb2xyz(r, g, b) {
michael@0 26 r /= 255;
michael@0 27 g /= 255;
michael@0 28 b /= 255;
michael@0 29
michael@0 30 // assume sRGB
michael@0 31 r = r > 0.04045 ? Math.pow(((r + 0.055) / 1.055), 2.4) : (r / 12.92);
michael@0 32 g = g > 0.04045 ? Math.pow(((g + 0.055) / 1.055), 2.4) : (g / 12.92);
michael@0 33 b = b > 0.04045 ? Math.pow(((b + 0.055) / 1.055), 2.4) : (b / 12.92);
michael@0 34
michael@0 35 return {
michael@0 36 x: ((r * 0.4124) + (g * 0.3576) + (b * 0.1805)) * 100,
michael@0 37 y: ((r * 0.2126) + (g * 0.7152) + (b * 0.0722)) * 100,
michael@0 38 z: ((r * 0.0193) + (g * 0.1192) + (b * 0.9505)) * 100
michael@0 39 };
michael@0 40 }
michael@0 41
michael@0 42 /**
michael@0 43 * Given the RGB components of a color as integers from 0-255, return the
michael@0 44 * color in the Lab color space.
michael@0 45 *
michael@0 46 * @return An object with lightness, a, b properties holding those components
michael@0 47 * of the color in the Lab color space.
michael@0 48 */
michael@0 49 function rgb2lab(r, g, b) {
michael@0 50 let xyz = rgb2xyz(r, g, b),
michael@0 51 x = xyz.x / 95.047,
michael@0 52 y = xyz.y / 100,
michael@0 53 z = xyz.z / 108.883;
michael@0 54
michael@0 55 x = x > 0.008856 ? Math.pow(x, 1/3) : (7.787 * x) + (16 / 116);
michael@0 56 y = y > 0.008856 ? Math.pow(y, 1/3) : (7.787 * y) + (16 / 116);
michael@0 57 z = z > 0.008856 ? Math.pow(z, 1/3) : (7.787 * z) + (16 / 116);
michael@0 58
michael@0 59 return {
michael@0 60 lightness: (116 * y) - 16,
michael@0 61 a: 500 * (x - y),
michael@0 62 b: 200 * (y - z)
michael@0 63 };
michael@0 64 }

mercurial