2013-09-16 03:01:25 -07:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2013-10-17 15:51:10 -07:00
|
|
|
const {Cc, Ci, Cu} = require("chrome");
|
2013-10-18 07:01:20 -07:00
|
|
|
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
|
|
|
|
|
|
|
|
const COLOR_UNIT_PREF = "devtools.defaultColorUnit";
|
2013-09-18 03:34:44 -07:00
|
|
|
|
2013-10-16 07:04:47 -07:00
|
|
|
const SPECIALVALUES = new Set([
|
|
|
|
"currentcolor",
|
|
|
|
"initial",
|
|
|
|
"inherit",
|
|
|
|
"transparent",
|
|
|
|
"unset"
|
|
|
|
]);
|
|
|
|
|
2013-09-16 03:01:25 -07:00
|
|
|
/**
|
|
|
|
* This module is used to convert between various color types.
|
|
|
|
*
|
|
|
|
* Usage:
|
2015-10-13 16:18:43 -07:00
|
|
|
* let {require} = Cu.import("resource://devtools/shared/Loader.jsm", {});
|
2015-09-21 10:04:18 -07:00
|
|
|
* let {colorUtils} = require("devtools/shared/css-color");
|
2013-10-18 07:01:20 -07:00
|
|
|
* let color = new colorUtils.CssColor("red");
|
2013-09-16 03:01:25 -07:00
|
|
|
*
|
|
|
|
* color.authored === "red"
|
|
|
|
* color.hasAlpha === false
|
|
|
|
* color.valid === true
|
|
|
|
* color.transparent === false // transparent has a special status.
|
|
|
|
* color.name === "red" // returns hex or rgba when no name available.
|
2015-10-07 22:54:00 -07:00
|
|
|
* color.hex === "#f00" // returns shortHex when available else returns
|
2013-09-16 03:01:25 -07:00
|
|
|
* longHex. If alpha channel is present then we
|
|
|
|
* return this.rgba.
|
2015-10-07 22:54:00 -07:00
|
|
|
* color.longHex === "#ff0000" // If alpha channel is present then we return
|
2013-09-16 03:01:25 -07:00
|
|
|
* this.rgba.
|
|
|
|
* color.rgb === "rgb(255, 0, 0)" // If alpha channel is present then we return
|
|
|
|
* this.rgba.
|
|
|
|
* color.rgba === "rgba(255, 0, 0, 1)"
|
|
|
|
* color.hsl === "hsl(0, 100%, 50%)"
|
|
|
|
* color.hsla === "hsla(0, 100%, 50%, 1)" // If alpha channel is present
|
|
|
|
* then we return this.rgba.
|
|
|
|
*
|
2015-10-07 22:54:00 -07:00
|
|
|
* color.toString() === "#f00"; // Outputs the color type determined in the
|
2013-09-16 03:01:25 -07:00
|
|
|
* COLOR_UNIT_PREF constant (above).
|
|
|
|
* // Color objects can be reused
|
2015-10-07 22:54:00 -07:00
|
|
|
* color.newColor("green") === "#0f0"; // true
|
2013-09-16 03:01:25 -07:00
|
|
|
*
|
|
|
|
* Valid values for COLOR_UNIT_PREF are contained in CssColor.COLORUNIT.
|
|
|
|
*/
|
|
|
|
|
|
|
|
function CssColor(colorValue) {
|
|
|
|
this.newColor(colorValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.colorUtils = {
|
|
|
|
CssColor: CssColor,
|
2015-01-05 11:57:00 -08:00
|
|
|
rgbToHsl: rgbToHsl,
|
2015-09-01 04:26:00 -07:00
|
|
|
setAlpha: setAlpha,
|
|
|
|
classifyColor: classifyColor
|
2013-09-16 03:01:25 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Values used in COLOR_UNIT_PREF
|
|
|
|
*/
|
|
|
|
CssColor.COLORUNIT = {
|
|
|
|
"authored": "authored",
|
|
|
|
"hex": "hex",
|
|
|
|
"name": "name",
|
|
|
|
"rgb": "rgb",
|
|
|
|
"hsl": "hsl"
|
|
|
|
};
|
|
|
|
|
|
|
|
CssColor.prototype = {
|
2015-04-17 03:53:06 -07:00
|
|
|
_colorUnit: null,
|
2015-10-07 22:54:00 -07:00
|
|
|
_colorUnitUppercase: false,
|
2015-04-17 03:53:06 -07:00
|
|
|
|
2015-09-01 04:26:00 -07:00
|
|
|
// The value as-authored.
|
2013-09-16 03:01:25 -07:00
|
|
|
authored: null,
|
2015-09-01 04:26:00 -07:00
|
|
|
// A lower-cased copy of |authored|.
|
|
|
|
lowerCased: null,
|
2013-09-16 03:01:25 -07:00
|
|
|
|
2015-04-17 03:53:06 -07:00
|
|
|
get colorUnit() {
|
|
|
|
if (this._colorUnit === null) {
|
|
|
|
let defaultUnit = Services.prefs.getCharPref(COLOR_UNIT_PREF);
|
|
|
|
this._colorUnit = CssColor.COLORUNIT[defaultUnit];
|
2015-10-07 22:54:00 -07:00
|
|
|
this._colorUnitUppercase =
|
|
|
|
(this.authored === this.authored.toUpperCase());
|
2015-04-17 03:53:06 -07:00
|
|
|
}
|
|
|
|
return this._colorUnit;
|
|
|
|
},
|
|
|
|
|
|
|
|
set colorUnit(unit) {
|
|
|
|
this._colorUnit = unit;
|
|
|
|
},
|
|
|
|
|
2015-10-07 03:04:00 -07:00
|
|
|
/**
|
|
|
|
* If the current color unit pref is "authored", then set the
|
|
|
|
* default color unit from the given color. Otherwise, leave the
|
|
|
|
* color unit untouched.
|
|
|
|
*
|
|
|
|
* @param {String} color The color to use
|
|
|
|
*/
|
|
|
|
setAuthoredUnitFromColor: function(color) {
|
|
|
|
if (Services.prefs.getCharPref(COLOR_UNIT_PREF) ===
|
|
|
|
CssColor.COLORUNIT.authored) {
|
|
|
|
this._colorUnit = classifyColor(color);
|
2015-10-07 22:54:00 -07:00
|
|
|
this._colorUnitUppercase = (color === color.toUpperCase());
|
2015-10-07 03:04:00 -07:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-16 03:01:25 -07:00
|
|
|
get hasAlpha() {
|
2013-10-16 07:04:47 -07:00
|
|
|
if (!this.valid) {
|
2013-09-16 03:01:25 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return this._getRGBATuple().a !== 1;
|
|
|
|
},
|
|
|
|
|
|
|
|
get valid() {
|
2014-12-17 05:43:30 -08:00
|
|
|
return DOMUtils.isValidCSSColor(this.authored);
|
2013-09-16 03:01:25 -07:00
|
|
|
},
|
|
|
|
|
2013-10-16 07:04:47 -07:00
|
|
|
/**
|
|
|
|
* Return true for all transparent values e.g. rgba(0, 0, 0, 0).
|
|
|
|
*/
|
2013-09-16 03:01:25 -07:00
|
|
|
get transparent() {
|
|
|
|
try {
|
|
|
|
let tuple = this._getRGBATuple();
|
2013-10-16 07:04:47 -07:00
|
|
|
return !(tuple.r || tuple.g || tuple.b || tuple.a);
|
2013-09-16 03:01:25 -07:00
|
|
|
} catch(e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-10-16 07:04:47 -07:00
|
|
|
get specialValue() {
|
2015-09-01 04:26:00 -07:00
|
|
|
return SPECIALVALUES.has(this.lowerCased) ? this.authored : null;
|
2013-10-16 07:04:47 -07:00
|
|
|
},
|
|
|
|
|
2013-09-16 03:01:25 -07:00
|
|
|
get name() {
|
2014-12-17 05:43:30 -08:00
|
|
|
let invalidOrSpecialValue = this._getInvalidOrSpecialValue();
|
|
|
|
if (invalidOrSpecialValue !== false) {
|
|
|
|
return invalidOrSpecialValue;
|
2013-09-16 03:01:25 -07:00
|
|
|
}
|
2013-10-16 07:04:47 -07:00
|
|
|
|
2013-09-16 03:01:25 -07:00
|
|
|
try {
|
|
|
|
let tuple = this._getRGBATuple();
|
|
|
|
|
|
|
|
if (tuple.a !== 1) {
|
|
|
|
return this.rgb;
|
|
|
|
}
|
|
|
|
let {r, g, b} = tuple;
|
|
|
|
return DOMUtils.rgbToColorName(r, g, b);
|
|
|
|
} catch(e) {
|
|
|
|
return this.hex;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get hex() {
|
2014-12-17 05:43:30 -08:00
|
|
|
let invalidOrSpecialValue = this._getInvalidOrSpecialValue();
|
|
|
|
if (invalidOrSpecialValue !== false) {
|
|
|
|
return invalidOrSpecialValue;
|
2013-10-16 07:04:47 -07:00
|
|
|
}
|
2013-09-16 03:01:25 -07:00
|
|
|
if (this.hasAlpha) {
|
|
|
|
return this.rgba;
|
|
|
|
}
|
|
|
|
|
|
|
|
let hex = this.longHex;
|
|
|
|
if (hex.charAt(1) == hex.charAt(2) &&
|
|
|
|
hex.charAt(3) == hex.charAt(4) &&
|
|
|
|
hex.charAt(5) == hex.charAt(6)) {
|
|
|
|
hex = "#" + hex.charAt(1) + hex.charAt(3) + hex.charAt(5);
|
|
|
|
}
|
|
|
|
return hex;
|
|
|
|
},
|
|
|
|
|
|
|
|
get longHex() {
|
2014-12-17 05:43:30 -08:00
|
|
|
let invalidOrSpecialValue = this._getInvalidOrSpecialValue();
|
|
|
|
if (invalidOrSpecialValue !== false) {
|
|
|
|
return invalidOrSpecialValue;
|
2013-10-16 07:04:47 -07:00
|
|
|
}
|
2013-09-16 03:01:25 -07:00
|
|
|
if (this.hasAlpha) {
|
|
|
|
return this.rgba;
|
|
|
|
}
|
2015-05-19 08:56:14 -07:00
|
|
|
|
|
|
|
let tuple = this._getRGBATuple();
|
2015-10-07 22:54:00 -07:00
|
|
|
return "#" + ((1 << 24) + (tuple.r << 16) + (tuple.g << 8) + (tuple.b << 0)).toString(16).substr(-6);
|
2013-09-16 03:01:25 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get rgb() {
|
2014-12-17 05:43:30 -08:00
|
|
|
let invalidOrSpecialValue = this._getInvalidOrSpecialValue();
|
|
|
|
if (invalidOrSpecialValue !== false) {
|
|
|
|
return invalidOrSpecialValue;
|
2013-09-16 03:01:25 -07:00
|
|
|
}
|
|
|
|
if (!this.hasAlpha) {
|
2015-09-01 04:26:00 -07:00
|
|
|
if (this.lowerCased.startsWith("rgb(")) {
|
|
|
|
// The color is valid and begins with rgb(.
|
2013-09-18 03:34:44 -07:00
|
|
|
return this.authored;
|
|
|
|
}
|
2013-09-16 03:01:25 -07:00
|
|
|
let tuple = this._getRGBATuple();
|
|
|
|
return "rgb(" + tuple.r + ", " + tuple.g + ", " + tuple.b + ")";
|
|
|
|
}
|
|
|
|
return this.rgba;
|
|
|
|
},
|
|
|
|
|
|
|
|
get rgba() {
|
2014-12-17 05:43:30 -08:00
|
|
|
let invalidOrSpecialValue = this._getInvalidOrSpecialValue();
|
|
|
|
if (invalidOrSpecialValue !== false) {
|
|
|
|
return invalidOrSpecialValue;
|
2013-09-16 03:01:25 -07:00
|
|
|
}
|
2015-09-01 04:26:00 -07:00
|
|
|
if (this.lowerCased.startsWith("rgba(")) {
|
|
|
|
// The color is valid and begins with rgba(.
|
2013-09-18 03:34:44 -07:00
|
|
|
return this.authored;
|
|
|
|
}
|
2013-09-16 03:01:25 -07:00
|
|
|
let components = this._getRGBATuple();
|
|
|
|
return "rgba(" + components.r + ", " +
|
|
|
|
components.g + ", " +
|
|
|
|
components.b + ", " +
|
|
|
|
components.a + ")";
|
|
|
|
},
|
|
|
|
|
|
|
|
get hsl() {
|
2014-12-17 05:43:30 -08:00
|
|
|
let invalidOrSpecialValue = this._getInvalidOrSpecialValue();
|
|
|
|
if (invalidOrSpecialValue !== false) {
|
|
|
|
return invalidOrSpecialValue;
|
2013-09-16 03:01:25 -07:00
|
|
|
}
|
2015-09-01 04:26:00 -07:00
|
|
|
if (this.lowerCased.startsWith("hsl(")) {
|
|
|
|
// The color is valid and begins with hsl(.
|
2013-09-18 03:34:44 -07:00
|
|
|
return this.authored;
|
|
|
|
}
|
2013-09-16 03:01:25 -07:00
|
|
|
if (this.hasAlpha) {
|
|
|
|
return this.hsla;
|
|
|
|
}
|
2015-05-19 08:56:14 -07:00
|
|
|
return this._hsl();
|
2013-09-16 03:01:25 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
get hsla() {
|
2014-12-17 05:43:30 -08:00
|
|
|
let invalidOrSpecialValue = this._getInvalidOrSpecialValue();
|
|
|
|
if (invalidOrSpecialValue !== false) {
|
|
|
|
return invalidOrSpecialValue;
|
2013-09-16 03:01:25 -07:00
|
|
|
}
|
2015-09-01 04:26:00 -07:00
|
|
|
if (this.lowerCased.startsWith("hsla(")) {
|
|
|
|
// The color is valid and begins with hsla(.
|
2013-09-18 03:34:44 -07:00
|
|
|
return this.authored;
|
2013-09-16 03:01:25 -07:00
|
|
|
}
|
|
|
|
if (this.hasAlpha) {
|
|
|
|
let a = this._getRGBATuple().a;
|
2015-05-19 08:56:14 -07:00
|
|
|
return this._hsl(a);
|
2013-09-16 03:01:25 -07:00
|
|
|
}
|
2015-05-19 08:56:14 -07:00
|
|
|
return this._hsl(1);
|
2013-09-16 03:01:25 -07:00
|
|
|
},
|
|
|
|
|
2014-12-17 05:43:30 -08:00
|
|
|
/**
|
|
|
|
* Check whether the current color value is in the special list e.g.
|
|
|
|
* transparent or invalid.
|
|
|
|
*
|
|
|
|
* @return {String|Boolean}
|
|
|
|
* - If the current color is a special value e.g. "transparent" then
|
|
|
|
* return the color.
|
|
|
|
* - If the color is invalid return an empty string.
|
|
|
|
* - If the color is a regular color e.g. #F06 so we return false
|
|
|
|
* to indicate that the color is neither invalid or special.
|
|
|
|
*/
|
|
|
|
_getInvalidOrSpecialValue: function() {
|
|
|
|
if (this.specialValue) {
|
|
|
|
return this.specialValue;
|
|
|
|
}
|
|
|
|
if (!this.valid) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2013-09-16 03:01:25 -07:00
|
|
|
/**
|
|
|
|
* Change color
|
|
|
|
*
|
|
|
|
* @param {String} color
|
|
|
|
* Any valid color string
|
|
|
|
*/
|
|
|
|
newColor: function(color) {
|
2015-09-01 04:26:00 -07:00
|
|
|
// Store a lower-cased version of the color to help with format
|
|
|
|
// testing. The original text is kept as well so it can be
|
|
|
|
// returned when needed.
|
|
|
|
this.lowerCased = color.toLowerCase();
|
|
|
|
this.authored = color;
|
2013-09-16 03:01:25 -07:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2015-04-17 03:53:06 -07:00
|
|
|
nextColorUnit: function() {
|
|
|
|
// Reorder the formats array to have the current format at the
|
|
|
|
// front so we can cycle through.
|
|
|
|
let formats = ["authored", "hex", "hsl", "rgb", "name"];
|
|
|
|
let putOnEnd = formats.splice(0, formats.indexOf(this.colorUnit));
|
|
|
|
formats = formats.concat(putOnEnd);
|
|
|
|
let currentDisplayedColor = this[formats[0]];
|
|
|
|
|
|
|
|
for (let format of formats) {
|
|
|
|
if (this[format].toLowerCase() !== currentDisplayedColor.toLowerCase()) {
|
|
|
|
this.colorUnit = CssColor.COLORUNIT[format];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.toString();
|
|
|
|
},
|
|
|
|
|
2013-09-16 03:01:25 -07:00
|
|
|
/**
|
|
|
|
* Return a string representing a color of type defined in COLOR_UNIT_PREF.
|
|
|
|
*/
|
|
|
|
toString: function() {
|
|
|
|
let color;
|
|
|
|
|
2015-04-17 03:53:06 -07:00
|
|
|
switch(this.colorUnit) {
|
2013-09-16 03:01:25 -07:00
|
|
|
case CssColor.COLORUNIT.authored:
|
|
|
|
color = this.authored;
|
|
|
|
break;
|
|
|
|
case CssColor.COLORUNIT.hex:
|
|
|
|
color = this.hex;
|
|
|
|
break;
|
|
|
|
case CssColor.COLORUNIT.hsl:
|
|
|
|
color = this.hsl;
|
|
|
|
break;
|
|
|
|
case CssColor.COLORUNIT.name:
|
|
|
|
color = this.name;
|
|
|
|
break;
|
|
|
|
case CssColor.COLORUNIT.rgb:
|
|
|
|
color = this.rgb;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
color = this.rgb;
|
|
|
|
}
|
2015-10-07 22:54:00 -07:00
|
|
|
|
|
|
|
if (this._colorUnitUppercase &&
|
|
|
|
this.colorUnit != CssColor.COLORUNIT.authored) {
|
|
|
|
color = color.toUpperCase();
|
|
|
|
}
|
|
|
|
|
2013-09-16 03:01:25 -07:00
|
|
|
return color;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a RGBA 4-Tuple representation of a color or transparent as
|
|
|
|
* appropriate.
|
|
|
|
*/
|
|
|
|
_getRGBATuple: function() {
|
2014-12-17 05:43:30 -08:00
|
|
|
let tuple = DOMUtils.colorToRGBA(this.authored);
|
2013-09-16 03:01:25 -07:00
|
|
|
|
2014-12-17 05:43:30 -08:00
|
|
|
tuple.a = parseFloat(tuple.a.toFixed(1));
|
2013-09-16 03:01:25 -07:00
|
|
|
|
2014-12-17 05:43:30 -08:00
|
|
|
return tuple;
|
2013-09-16 03:01:25 -07:00
|
|
|
},
|
|
|
|
|
2015-05-19 08:56:14 -07:00
|
|
|
_hsl: function(maybeAlpha) {
|
2015-09-01 04:26:00 -07:00
|
|
|
if (this.lowerCased.startsWith("hsl(") && maybeAlpha === undefined) {
|
2015-05-19 08:56:14 -07:00
|
|
|
// We can use it as-is.
|
|
|
|
return this.authored;
|
2013-09-16 03:01:25 -07:00
|
|
|
}
|
|
|
|
|
2015-05-19 08:56:14 -07:00
|
|
|
let {r, g, b} = this._getRGBATuple();
|
2014-03-31 15:26:53 -07:00
|
|
|
let [h,s,l] = rgbToHsl([r,g,b]);
|
2015-05-19 08:56:14 -07:00
|
|
|
if (maybeAlpha !== undefined) {
|
|
|
|
return "hsla(" + h + ", " + s + "%, " + l + "%, " + maybeAlpha + ")";
|
|
|
|
} else {
|
|
|
|
return "hsl(" + h + ", " + s + "%, " + l + "%)";
|
|
|
|
}
|
2013-09-16 03:01:25 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method allows comparison of CssColor objects using ===.
|
|
|
|
*/
|
|
|
|
valueOf: function() {
|
|
|
|
return this.rgba;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2014-03-31 15:26:53 -07:00
|
|
|
/**
|
|
|
|
* Convert rgb value to hsl
|
|
|
|
*
|
|
|
|
* @param {array} rgb
|
|
|
|
* Array of rgb values
|
|
|
|
* @return {array}
|
|
|
|
* Array of hsl values.
|
|
|
|
*/
|
|
|
|
function rgbToHsl([r,g,b]) {
|
|
|
|
r = r / 255;
|
|
|
|
g = g / 255;
|
|
|
|
b = b / 255;
|
|
|
|
|
|
|
|
let max = Math.max(r, g, b);
|
|
|
|
let min = Math.min(r, g, b);
|
|
|
|
let h;
|
|
|
|
let s;
|
|
|
|
let l = (max + min) / 2;
|
|
|
|
|
2015-09-01 11:25:13 -07:00
|
|
|
if (max == min){
|
2014-03-31 15:26:53 -07:00
|
|
|
h = s = 0;
|
|
|
|
} else {
|
|
|
|
let d = max - min;
|
|
|
|
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
|
|
|
|
|
|
switch(max) {
|
|
|
|
case r:
|
|
|
|
h = ((g - b) / d) % 6;
|
|
|
|
break;
|
|
|
|
case g:
|
|
|
|
h = (b - r) / d + 2;
|
|
|
|
break;
|
|
|
|
case b:
|
|
|
|
h = (r - g) / d + 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
h *= 60;
|
|
|
|
if (h < 0) {
|
|
|
|
h += 360;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [Math.round(h), Math.round(s * 100), Math.round(l * 100)];
|
|
|
|
}
|
|
|
|
|
2015-01-05 11:57:00 -08:00
|
|
|
/**
|
|
|
|
* Takes a color value of any type (hex, hsl, hsla, rgb, rgba)
|
|
|
|
* and an alpha value to generate an rgba string with the correct
|
|
|
|
* alpha value.
|
|
|
|
*
|
|
|
|
* @param {String} colorValue
|
|
|
|
* Color in the form of hex, hsl, hsla, rgb, rgba.
|
|
|
|
* @param {Number} alpha
|
|
|
|
* Alpha value for the color, between 0 and 1.
|
|
|
|
* @return {String}
|
|
|
|
* Converted color with `alpha` value in rgba form.
|
|
|
|
*/
|
|
|
|
function setAlpha(colorValue, alpha) {
|
|
|
|
let color = new CssColor(colorValue);
|
|
|
|
|
|
|
|
// Throw if the color supplied is not valid.
|
|
|
|
if (!color.valid) {
|
|
|
|
throw new Error("Invalid color.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an invalid alpha valid, just set to 1.
|
|
|
|
if (!(alpha >= 0 && alpha <= 1)) {
|
|
|
|
alpha = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let { r, g, b } = color._getRGBATuple();
|
|
|
|
return "rgba(" + r + ", " + g + ", " + b + ", " + alpha + ")";
|
|
|
|
}
|
|
|
|
|
2015-09-01 04:26:00 -07:00
|
|
|
/**
|
|
|
|
* Given a color, classify its type as one of the possible color
|
|
|
|
* units, as known by |CssColor.colorUnit|.
|
|
|
|
*
|
|
|
|
* @param {String} value
|
|
|
|
* The color, in any form accepted by CSS.
|
|
|
|
* @return {String}
|
|
|
|
* The color classification, one of "rgb", "hsl", "hex", or "name".
|
|
|
|
*/
|
|
|
|
function classifyColor(value) {
|
|
|
|
value = value.toLowerCase();
|
|
|
|
if (value.startsWith("rgb(") || value.startsWith("rgba(")) {
|
|
|
|
return CssColor.COLORUNIT.rgb;
|
|
|
|
} else if (value.startsWith("hsl(") || value.startsWith("hsla(")) {
|
|
|
|
return CssColor.COLORUNIT.hsl;
|
|
|
|
} else if (/^#[0-9a-f]+$/.exec(value)) {
|
|
|
|
return CssColor.COLORUNIT.hex;
|
|
|
|
}
|
|
|
|
return CssColor.COLORUNIT.name;
|
|
|
|
}
|
|
|
|
|
2013-09-16 03:01:25 -07:00
|
|
|
loader.lazyGetter(this, "DOMUtils", function () {
|
|
|
|
return Cc["@mozilla.org/inspector/dom-utils;1"].getService(Ci.inIDOMUtils);
|
|
|
|
});
|