Path: blob/trunk/javascript/selenium-webdriver/lib/color.js
8668 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819/**20* @fileoverview Color parsing and formatting utilities mirroring Selenium's Java Color.21*/2223class Color {24/**25* @param {number} red26* @param {number} green27* @param {number} blue28* @param {number} alpha29*/30constructor(red, green, blue, alpha = 1) {31this.red_ = Color.#clamp255(red)32this.green_ = Color.#clamp255(green)33this.blue_ = Color.#clamp255(blue)34this.alpha_ = Color.#clamp01(alpha)35}3637/**38* Guesses the input color format and returns a Color instance.39* @param {string} value40* @returns {Color}41*/42static fromString(value) {43const v = String(value)44for (const conv of [45Color.#fromRgb,46Color.#fromRgbPct,47Color.#fromRgba,48Color.#fromRgbaPct,49Color.#fromHex6,50Color.#fromHex3,51Color.#fromHsl,52Color.#fromHsla,53Color.#fromNamed,54]) {55const c = conv(v)56if (c) return c57}58throw new Error(`Did not know how to convert ${value} into color`)59}6061/**62* Sets opacity (alpha channel).63* @param {number} alpha64*/65setOpacity(alpha) {66this.alpha_ = Color.#clamp01(alpha)67}6869/**70* @returns {string} e.g. "rgb(255, 0, 0)"71*/72asRgb() {73return `rgb(${this.red_}, ${this.green_}, ${this.blue_})`74}7576/**77* @returns {string} e.g. "rgba(255, 0, 0, 1)"78*/79asRgba() {80let a81if (this.alpha_ === 1) {82a = '1'83} else if (this.alpha_ === 0) {84a = '0'85} else {86a = String(this.alpha_)87}88return `rgba(${this.red_}, ${this.green_}, ${this.blue_}, ${a})`89}9091/**92* @returns {string} e.g. "#ff0000"93*/94asHex() {95const toHex = (n) => n.toString(16).padStart(2, '0')96return `#${toHex(this.red_)}${toHex(this.green_)}${toHex(this.blue_)}`97}9899/** @override */100toString() {101return `Color: ${this.asRgba()}`102}103104/**105* @param {*} other106* @returns {boolean}107*/108equals(other) {109return other instanceof Color && this.asRgba() === other.asRgba()110}111112// Converters113static #fromRgb(v) {114const m = /^\s*rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)\s*$/i.exec(v)115return m ? new Color(+m[1], +m[2], +m[3], 1) : null116}117118static #fromRgbPct(v) {119const m =120/^\s*rgb\(\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(\d{1,3}|\d{1,2}\.\d+)%\s*\)\s*$/i.exec(121v,122)123if (!m) return null124const pct = (i) => Math.floor((Math.min(100, Math.max(0, parseFloat(m[i]))) / 100) * 255)125return new Color(pct(1), pct(2), pct(3), 1)126}127128static #fromRgba(v) {129const m = /^\s*rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(0|1|0\.\d+)\s*\)\s*$/i.exec(v)130return m ? new Color(+m[1], +m[2], +m[3], parseFloat(m[4])) : null131}132133static #fromRgbaPct(v) {134const m =135/^\s*rgba\(\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(\d{1,3}|\d{1,2}\.\d+)%\s*,\s*(0|1|0\.\d+)\s*\)\s*$/i.exec(136v,137)138if (!m) return null139const pct = (i) => Math.floor((Math.min(100, Math.max(0, parseFloat(m[i]))) / 100) * 255)140return new Color(pct(1), pct(2), pct(3), parseFloat(m[4]))141}142143static #fromHex6(v) {144const m = /^#([\da-f]{2})([\da-f]{2})([\da-f]{2})$/i.exec(v)145return m ? new Color(parseInt(m[1], 16), parseInt(m[2], 16), parseInt(m[3], 16), 1) : null146}147148static #fromHex3(v) {149const m = /^#([\da-f])([\da-f])([\da-f])$/i.exec(v)150return m ? new Color(parseInt(m[1] + m[1], 16), parseInt(m[2] + m[2], 16), parseInt(m[3] + m[3], 16), 1) : null151}152153static #fromHsl(v) {154const m = /^\s*hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)\s*$/i.exec(v)155return m ? Color.#hslToColor(+m[1], +m[2] / 100, +m[3] / 100, 1) : null156}157158static #fromHsla(v) {159const m = /^\s*hsla\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*,\s*(0|1|0\.\d+)\s*\)\s*$/i.exec(v)160return m ? Color.#hslToColor(+m[1], +m[2] / 100, +m[3] / 100, parseFloat(m[4])) : null161}162163static #hslToColor(hDeg, s, l, a) {164const h = (((hDeg % 360) + 360) % 360) / 360165if (s === 0) {166const v = Math.round(l * 255)167return new Color(v, v, v, a)168}169const luminocity2 = l < 0.5 ? l * (1 + s) : l + s - l * s170const luminocity1 = 2 * l - luminocity2171const hueToRgb = (l1, l2, hue) => {172if (hue < 0) hue += 1173if (hue > 1) hue -= 1174if (hue < 1 / 6) return l1 + (l2 - l1) * 6 * hue175if (hue < 1 / 2) return l2176if (hue < 2 / 3) return l1 + (l2 - l1) * (2 / 3 - hue) * 6177return l1178}179const r = Math.round(hueToRgb(luminocity1, luminocity2, h + 1 / 3) * 255)180const g = Math.round(hueToRgb(luminocity1, luminocity2, h) * 255)181const b = Math.round(hueToRgb(luminocity1, luminocity2, h - 1 / 3) * 255)182return new Color(r, g, b, a)183}184185static #fromNamed(v) {186const name = String(v).trim().toLowerCase()187const c = Colors[name]188return c ? new Color(c.red_, c.green_, c.blue_, c.alpha_) : null189}190191static #clamp255(n) {192return Math.max(0, Math.min(255, Math.round(n)))193}194195static #clamp01(n) {196return Math.max(0, Math.min(1, n))197}198}199200// Basic colour keywords as defined by the W3C HTML/CSS spec.201// Keys are lowercase to match typical CSS usage.202const Colors = {203transparent: new Color(0, 0, 0, 0),204aliceblue: new Color(240, 248, 255, 1),205antiquewhite: new Color(250, 235, 215, 1),206aqua: new Color(0, 255, 255, 1),207aquamarine: new Color(127, 255, 212, 1),208azure: new Color(240, 255, 255, 1),209beige: new Color(245, 245, 220, 1),210bisque: new Color(255, 228, 196, 1),211black: new Color(0, 0, 0, 1),212blanchedalmond: new Color(255, 235, 205, 1),213blue: new Color(0, 0, 255, 1),214blueviolet: new Color(138, 43, 226, 1),215brown: new Color(165, 42, 42, 1),216burlywood: new Color(222, 184, 135, 1),217cadetblue: new Color(95, 158, 160, 1),218chartreuse: new Color(127, 255, 0, 1),219chocolate: new Color(210, 105, 30, 1),220coral: new Color(255, 127, 80, 1),221cornflowerblue: new Color(100, 149, 237, 1),222cornsilk: new Color(255, 248, 220, 1),223crimson: new Color(220, 20, 60, 1),224cyan: new Color(0, 255, 255, 1),225darkblue: new Color(0, 0, 139, 1),226darkcyan: new Color(0, 139, 139, 1),227darkgoldenrod: new Color(184, 134, 11, 1),228darkgray: new Color(169, 169, 169, 1),229darkgreen: new Color(0, 100, 0, 1),230darkgrey: new Color(169, 169, 169, 1),231darkkhaki: new Color(189, 183, 107, 1),232darkmagenta: new Color(139, 0, 139, 1),233darkolivegreen: new Color(85, 107, 47, 1),234darkorange: new Color(255, 140, 0, 1),235darkorchid: new Color(153, 50, 204, 1),236darkred: new Color(139, 0, 0, 1),237darksalmon: new Color(233, 150, 122, 1),238darkseagreen: new Color(143, 188, 143, 1),239darkslateblue: new Color(72, 61, 139, 1),240darkslategray: new Color(47, 79, 79, 1),241darkslategrey: new Color(47, 79, 79, 1),242darkturquoise: new Color(0, 206, 209, 1),243darkviolet: new Color(148, 0, 211, 1),244deeppink: new Color(255, 20, 147, 1),245deepskyblue: new Color(0, 191, 255, 1),246dimgray: new Color(105, 105, 105, 1),247dimgrey: new Color(105, 105, 105, 1),248dodgerblue: new Color(30, 144, 255, 1),249firebrick: new Color(178, 34, 34, 1),250floralwhite: new Color(255, 250, 240, 1),251forestgreen: new Color(34, 139, 34, 1),252fuchsia: new Color(255, 0, 255, 1),253gainsboro: new Color(220, 220, 220, 1),254ghostwhite: new Color(248, 248, 255, 1),255gold: new Color(255, 215, 0, 1),256goldenrod: new Color(218, 165, 32, 1),257gray: new Color(128, 128, 128, 1),258grey: new Color(128, 128, 128, 1),259green: new Color(0, 128, 0, 1),260greenyellow: new Color(173, 255, 47, 1),261honeydew: new Color(240, 255, 240, 1),262hotpink: new Color(255, 105, 180, 1),263indianred: new Color(205, 92, 92, 1),264indigo: new Color(75, 0, 130, 1),265ivory: new Color(255, 255, 240, 1),266khaki: new Color(240, 230, 140, 1),267lavender: new Color(230, 230, 250, 1),268lavenderblush: new Color(255, 240, 245, 1),269lawngreen: new Color(124, 252, 0, 1),270lemonchiffon: new Color(255, 250, 205, 1),271lightblue: new Color(173, 216, 230, 1),272lightcoral: new Color(240, 128, 128, 1),273lightcyan: new Color(224, 255, 255, 1),274lightgoldenrodyellow: new Color(250, 250, 210, 1),275lightgray: new Color(211, 211, 211, 1),276lightgreen: new Color(144, 238, 144, 1),277lightgrey: new Color(211, 211, 211, 1),278lightpink: new Color(255, 182, 193, 1),279lightsalmon: new Color(255, 160, 122, 1),280lightseagreen: new Color(32, 178, 170, 1),281lightskyblue: new Color(135, 206, 250, 1),282lightslategray: new Color(119, 136, 153, 1),283lightslategrey: new Color(119, 136, 153, 1),284lightsteelblue: new Color(176, 196, 222, 1),285lightyellow: new Color(255, 255, 224, 1),286lime: new Color(0, 255, 0, 1),287limegreen: new Color(50, 205, 50, 1),288linen: new Color(250, 240, 230, 1),289magenta: new Color(255, 0, 255, 1),290maroon: new Color(128, 0, 0, 1),291mediumaquamarine: new Color(102, 205, 170, 1),292mediumblue: new Color(0, 0, 205, 1),293mediumorchid: new Color(186, 85, 211, 1),294mediumpurple: new Color(147, 112, 219, 1),295mediumseagreen: new Color(60, 179, 113, 1),296mediumslateblue: new Color(123, 104, 238, 1),297mediumspringgreen: new Color(0, 250, 154, 1),298mediumturquoise: new Color(72, 209, 204, 1),299mediumvioletred: new Color(199, 21, 133, 1),300midnightblue: new Color(25, 25, 112, 1),301mintcream: new Color(245, 255, 250, 1),302mistyrose: new Color(255, 228, 225, 1),303moccasin: new Color(255, 228, 181, 1),304navajowhite: new Color(255, 222, 173, 1),305navy: new Color(0, 0, 128, 1),306oldlace: new Color(253, 245, 230, 1),307olive: new Color(128, 128, 0, 1),308olivedrab: new Color(107, 142, 35, 1),309orange: new Color(255, 165, 0, 1),310orangered: new Color(255, 69, 0, 1),311orchid: new Color(218, 112, 214, 1),312palegoldenrod: new Color(238, 232, 170, 1),313palegreen: new Color(152, 251, 152, 1),314paleturquoise: new Color(175, 238, 238, 1),315palevioletred: new Color(219, 112, 147, 1),316papayawhip: new Color(255, 239, 213, 1),317peachpuff: new Color(255, 218, 185, 1),318peru: new Color(205, 133, 63, 1),319pink: new Color(255, 192, 203, 1),320plum: new Color(221, 160, 221, 1),321powderblue: new Color(176, 224, 230, 1),322purple: new Color(128, 0, 128, 1),323rebeccapurple: new Color(102, 51, 153, 1),324red: new Color(255, 0, 0, 1),325rosybrown: new Color(188, 143, 143, 1),326royalblue: new Color(65, 105, 225, 1),327saddlebrown: new Color(139, 69, 19, 1),328salmon: new Color(250, 128, 114, 1),329sandybrown: new Color(244, 164, 96, 1),330seagreen: new Color(46, 139, 87, 1),331seashell: new Color(255, 245, 238, 1),332sienna: new Color(160, 82, 45, 1),333silver: new Color(192, 192, 192, 1),334skyblue: new Color(135, 206, 235, 1),335slateblue: new Color(106, 90, 205, 1),336slategray: new Color(112, 128, 144, 1),337slategrey: new Color(112, 128, 144, 1),338snow: new Color(255, 250, 250, 1),339springgreen: new Color(0, 255, 127, 1),340steelblue: new Color(70, 130, 180, 1),341tan: new Color(210, 180, 140, 1),342teal: new Color(0, 128, 128, 1),343thistle: new Color(216, 191, 216, 1),344tomato: new Color(255, 99, 71, 1),345turquoise: new Color(64, 224, 208, 1),346violet: new Color(238, 130, 238, 1),347wheat: new Color(245, 222, 179, 1),348white: new Color(255, 255, 255, 1),349whitesmoke: new Color(245, 245, 245, 1),350yellow: new Color(255, 255, 0, 1),351yellowgreen: new Color(154, 205, 50, 1),352}353354module.exports = {355Color,356Colors,357}358359360