| 1 |
/** |
| 2 |
* Copyright (c) 2019 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { IColor, IColorRGB } from 'common/Types'; |
| 7 |
|
| 8 |
let $r = 0; |
| 9 |
let $g = 0; |
| 10 |
let $b = 0; |
| 11 |
let $a = 0; |
| 12 |
|
| 13 |
export const NULL_COLOR: IColor = { |
| 14 |
css: '#00000000', |
| 15 |
rgba: 0 |
| 16 |
}; |
| 17 |
|
| 18 |
/** |
| 19 |
* Helper functions where the source type is "channels" (individual color channels as numbers). |
| 20 |
*/ |
| 21 |
export namespace channels { |
| 22 |
export function toCss(r: number, g: number, b: number, a?: number): string { |
| 23 |
if (a !== undefined) { |
| 24 |
return `#${toPaddedHex(r)}${toPaddedHex(g)}${toPaddedHex(b)}${toPaddedHex(a)}`; |
| 25 |
} |
| 26 |
return `#${toPaddedHex(r)}${toPaddedHex(g)}${toPaddedHex(b)}`; |
| 27 |
} |
| 28 |
|
| 29 |
export function toRgba(r: number, g: number, b: number, a: number = 0xFF): number { |
| 30 |
// Note: The aggregated number is RGBA32 (BE), thus needs to be converted to ABGR32 |
| 31 |
// on LE systems, before it can be used for direct 32-bit buffer writes. |
| 32 |
// >>> 0 forces an unsigned int |
| 33 |
return (r << 24 | g << 16 | b << 8 | a) >>> 0; |
| 34 |
} |
| 35 |
|
| 36 |
export function toColor(r: number, g: number, b: number, a?: number): IColor { |
| 37 |
return { |
| 38 |
css: channels.toCss(r, g, b, a), |
| 39 |
rgba: channels.toRgba(r, g, b, a) |
| 40 |
}; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Helper functions where the source type is `IColor`. |
| 46 |
*/ |
| 47 |
export namespace color { |
| 48 |
export function blend(bg: IColor, fg: IColor): IColor { |
| 49 |
$a = (fg.rgba & 0xFF) / 255; |
| 50 |
if ($a === 1) { |
| 51 |
return { |
| 52 |
css: fg.css, |
| 53 |
rgba: fg.rgba |
| 54 |
}; |
| 55 |
} |
| 56 |
const fgR = (fg.rgba >> 24) & 0xFF; |
| 57 |
const fgG = (fg.rgba >> 16) & 0xFF; |
| 58 |
const fgB = (fg.rgba >> 8) & 0xFF; |
| 59 |
const bgR = (bg.rgba >> 24) & 0xFF; |
| 60 |
const bgG = (bg.rgba >> 16) & 0xFF; |
| 61 |
const bgB = (bg.rgba >> 8) & 0xFF; |
| 62 |
$r = bgR + Math.round((fgR - bgR) * $a); |
| 63 |
$g = bgG + Math.round((fgG - bgG) * $a); |
| 64 |
$b = bgB + Math.round((fgB - bgB) * $a); |
| 65 |
const css = channels.toCss($r, $g, $b); |
| 66 |
const rgba = channels.toRgba($r, $g, $b); |
| 67 |
return { css, rgba }; |
| 68 |
} |
| 69 |
|
| 70 |
export function isOpaque(color: IColor): boolean { |
| 71 |
return (color.rgba & 0xFF) === 0xFF; |
| 72 |
} |
| 73 |
|
| 74 |
export function ensureContrastRatio(bg: IColor, fg: IColor, ratio: number): IColor | undefined { |
| 75 |
const result = rgba.ensureContrastRatio(bg.rgba, fg.rgba, ratio); |
| 76 |
if (!result) { |
| 77 |
return undefined; |
| 78 |
} |
| 79 |
return channels.toColor( |
| 80 |
(result >> 24 & 0xFF), |
| 81 |
(result >> 16 & 0xFF), |
| 82 |
(result >> 8 & 0xFF) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
export function opaque(color: IColor): IColor { |
| 87 |
const rgbaColor = (color.rgba | 0xFF) >>> 0; |
| 88 |
[$r, $g, $b] = rgba.toChannels(rgbaColor); |
| 89 |
return { |
| 90 |
css: channels.toCss($r, $g, $b), |
| 91 |
rgba: rgbaColor |
| 92 |
}; |
| 93 |
} |
| 94 |
|
| 95 |
export function opacity(color: IColor, opacity: number): IColor { |
| 96 |
$a = Math.round(opacity * 0xFF); |
| 97 |
[$r, $g, $b] = rgba.toChannels(color.rgba); |
| 98 |
return { |
| 99 |
css: channels.toCss($r, $g, $b, $a), |
| 100 |
rgba: channels.toRgba($r, $g, $b, $a) |
| 101 |
}; |
| 102 |
} |
| 103 |
|
| 104 |
export function multiplyOpacity(color: IColor, factor: number): IColor { |
| 105 |
$a = color.rgba & 0xFF; |
| 106 |
return opacity(color, ($a * factor) / 0xFF); |
| 107 |
} |
| 108 |
|
| 109 |
export function toColorRGB(color: IColor): IColorRGB { |
| 110 |
return [(color.rgba >> 24) & 0xFF, (color.rgba >> 16) & 0xFF, (color.rgba >> 8) & 0xFF]; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Helper functions where the source type is "css" (string: '#rgb', '#rgba', '#rrggbb', |
| 116 |
* '#rrggbbaa'). |
| 117 |
*/ |
| 118 |
export namespace css { |
| 119 |
// Attempt to set get the shared canvas context |
| 120 |
let $ctx: CanvasRenderingContext2D | undefined; |
| 121 |
let $litmusColor: CanvasGradient | undefined; |
| 122 |
try { |
| 123 |
// This is guaranteed to run in the first window, so document should be correct |
| 124 |
const canvas = document.createElement('canvas'); |
| 125 |
canvas.width = 1; |
| 126 |
canvas.height = 1; |
| 127 |
const ctx = canvas.getContext('2d', { |
| 128 |
willReadFrequently: true |
| 129 |
}); |
| 130 |
if (ctx) { |
| 131 |
$ctx = ctx; |
| 132 |
$ctx.globalCompositeOperation = 'copy'; |
| 133 |
$litmusColor = $ctx.createLinearGradient(0, 0, 1, 1); |
| 134 |
} |
| 135 |
} |
| 136 |
catch { |
| 137 |
// noop |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Converts a css string to an IColor, this should handle all valid CSS color strings and will |
| 142 |
* throw if it's invalid. The ideal format to use is `#rrggbb[aa]` as it's the fastest to parse. |
| 143 |
* |
| 144 |
* Only `#rgb[a]`, `#rrggbb[aa]`, `rgb()` and `rgba()` formats are supported when run in a Node |
| 145 |
* environment. |
| 146 |
*/ |
| 147 |
export function toColor(css: string): IColor { |
| 148 |
// Formats: #rgb[a] and #rrggbb[aa] |
| 149 |
if (css.match(/#[\da-f]{3,8}/i)) { |
| 150 |
switch (css.length) { |
| 151 |
case 4: { // #rgb |
| 152 |
$r = parseInt(css.slice(1, 2).repeat(2), 16); |
| 153 |
$g = parseInt(css.slice(2, 3).repeat(2), 16); |
| 154 |
$b = parseInt(css.slice(3, 4).repeat(2), 16); |
| 155 |
return channels.toColor($r, $g, $b); |
| 156 |
} |
| 157 |
case 5: { // #rgba |
| 158 |
$r = parseInt(css.slice(1, 2).repeat(2), 16); |
| 159 |
$g = parseInt(css.slice(2, 3).repeat(2), 16); |
| 160 |
$b = parseInt(css.slice(3, 4).repeat(2), 16); |
| 161 |
$a = parseInt(css.slice(4, 5).repeat(2), 16); |
| 162 |
return channels.toColor($r, $g, $b, $a); |
| 163 |
} |
| 164 |
case 7: // #rrggbb |
| 165 |
return { |
| 166 |
css, |
| 167 |
rgba: (parseInt(css.slice(1), 16) << 8 | 0xFF) >>> 0 |
| 168 |
}; |
| 169 |
case 9: // #rrggbbaa |
| 170 |
return { |
| 171 |
css, |
| 172 |
rgba: parseInt(css.slice(1), 16) >>> 0 |
| 173 |
}; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// Formats: rgb() or rgba() |
| 178 |
const rgbaMatch = css.match(/rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*(,\s*(0|1|\d?\.(\d+))\s*)?\)/); |
| 179 |
if (rgbaMatch) { |
| 180 |
$r = parseInt(rgbaMatch[1]); |
| 181 |
$g = parseInt(rgbaMatch[2]); |
| 182 |
$b = parseInt(rgbaMatch[3]); |
| 183 |
$a = Math.round((rgbaMatch[5] === undefined ? 1 : parseFloat(rgbaMatch[5])) * 0xFF); |
| 184 |
return channels.toColor($r, $g, $b, $a); |
| 185 |
} |
| 186 |
|
| 187 |
// Validate the context is available for canvas-based color parsing |
| 188 |
if (!$ctx || !$litmusColor) { |
| 189 |
throw new Error('css.toColor: Unsupported css format'); |
| 190 |
} |
| 191 |
|
| 192 |
// Validate the color using canvas fillStyle |
| 193 |
// See https://html.spec.whatwg.org/multipage/canvas.html#fill-and-stroke-styles |
| 194 |
$ctx.fillStyle = $litmusColor; |
| 195 |
$ctx.fillStyle = css; |
| 196 |
if (typeof $ctx.fillStyle !== 'string') { |
| 197 |
throw new Error('css.toColor: Unsupported css format'); |
| 198 |
} |
| 199 |
|
| 200 |
$ctx.fillRect(0, 0, 1, 1); |
| 201 |
[$r, $g, $b, $a] = $ctx.getImageData(0, 0, 1, 1).data; |
| 202 |
|
| 203 |
// Validate the color is non-transparent as color hue gets lost when drawn to the canvas |
| 204 |
if ($a !== 0xFF) { |
| 205 |
throw new Error('css.toColor: Unsupported css format'); |
| 206 |
} |
| 207 |
|
| 208 |
// Extract the color from the canvas' fillStyle property which exposes the color value in rgba() |
| 209 |
// format |
| 210 |
// See https://html.spec.whatwg.org/multipage/canvas.html#serialisation-of-a-color |
| 211 |
return { |
| 212 |
rgba: channels.toRgba($r, $g, $b, $a), |
| 213 |
css |
| 214 |
}; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Helper functions where the source type is "rgb" (number: 0xrrggbb). |
| 220 |
*/ |
| 221 |
export namespace rgb { |
| 222 |
/** |
| 223 |
* Gets the relative luminance of an RGB color, this is useful in determining the contrast ratio |
| 224 |
* between two colors. |
| 225 |
* @param rgb The color to use. |
| 226 |
* @see https://www.w3.org/TR/WCAG20/#relativeluminancedef |
| 227 |
*/ |
| 228 |
export function relativeLuminance(rgb: number): number { |
| 229 |
return relativeLuminance2( |
| 230 |
(rgb >> 16) & 0xFF, |
| 231 |
(rgb >> 8 ) & 0xFF, |
| 232 |
(rgb ) & 0xFF); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Gets the relative luminance of an RGB color, this is useful in determining the contrast ratio |
| 237 |
* between two colors. |
| 238 |
* @param r The red channel (0x00 to 0xFF). |
| 239 |
* @param g The green channel (0x00 to 0xFF). |
| 240 |
* @param b The blue channel (0x00 to 0xFF). |
| 241 |
* @see https://www.w3.org/TR/WCAG20/#relativeluminancedef |
| 242 |
*/ |
| 243 |
export function relativeLuminance2(r: number, g: number, b: number): number { |
| 244 |
const rs = r / 255; |
| 245 |
const gs = g / 255; |
| 246 |
const bs = b / 255; |
| 247 |
const rr = rs <= 0.03928 ? rs / 12.92 : Math.pow((rs + 0.055) / 1.055, 2.4); |
| 248 |
const rg = gs <= 0.03928 ? gs / 12.92 : Math.pow((gs + 0.055) / 1.055, 2.4); |
| 249 |
const rb = bs <= 0.03928 ? bs / 12.92 : Math.pow((bs + 0.055) / 1.055, 2.4); |
| 250 |
return rr * 0.2126 + rg * 0.7152 + rb * 0.0722; |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Helper functions where the source type is "rgba" (number: 0xrrggbbaa). |
| 256 |
*/ |
| 257 |
export namespace rgba { |
| 258 |
export function blend(bg: number, fg: number): number { |
| 259 |
$a = (fg & 0xFF) / 0xFF; |
| 260 |
if ($a === 1) { |
| 261 |
return fg; |
| 262 |
} |
| 263 |
const fgR = (fg >> 24) & 0xFF; |
| 264 |
const fgG = (fg >> 16) & 0xFF; |
| 265 |
const fgB = (fg >> 8) & 0xFF; |
| 266 |
const bgR = (bg >> 24) & 0xFF; |
| 267 |
const bgG = (bg >> 16) & 0xFF; |
| 268 |
const bgB = (bg >> 8) & 0xFF; |
| 269 |
$r = bgR + Math.round((fgR - bgR) * $a); |
| 270 |
$g = bgG + Math.round((fgG - bgG) * $a); |
| 271 |
$b = bgB + Math.round((fgB - bgB) * $a); |
| 272 |
return channels.toRgba($r, $g, $b); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Given a foreground color and a background color, either increase or reduce the luminance of the |
| 277 |
* foreground color until the specified contrast ratio is met. If pure white or black is hit |
| 278 |
* without the contrast ratio being met, go the other direction using the background color as the |
| 279 |
* foreground color and take either the first or second result depending on which has the higher |
| 280 |
* contrast ratio. |
| 281 |
* |
| 282 |
* `undefined` will be returned if the contrast ratio is already met. |
| 283 |
* |
| 284 |
* @param bgRgba The background color in rgba format. |
| 285 |
* @param fgRgba The foreground color in rgba format. |
| 286 |
* @param ratio The contrast ratio to achieve. |
| 287 |
*/ |
| 288 |
export function ensureContrastRatio(bgRgba: number, fgRgba: number, ratio: number): number | undefined { |
| 289 |
const bgL = rgb.relativeLuminance(bgRgba >> 8); |
| 290 |
const fgL = rgb.relativeLuminance(fgRgba >> 8); |
| 291 |
const cr = contrastRatio(bgL, fgL); |
| 292 |
if (cr < ratio) { |
| 293 |
if (fgL < bgL) { |
| 294 |
const resultA = reduceLuminance(bgRgba, fgRgba, ratio); |
| 295 |
const resultARatio = contrastRatio(bgL, rgb.relativeLuminance(resultA >> 8)); |
| 296 |
if (resultARatio < ratio) { |
| 297 |
const resultB = increaseLuminance(bgRgba, fgRgba, ratio); |
| 298 |
const resultBRatio = contrastRatio(bgL, rgb.relativeLuminance(resultB >> 8)); |
| 299 |
return resultARatio > resultBRatio ? resultA : resultB; |
| 300 |
} |
| 301 |
return resultA; |
| 302 |
} |
| 303 |
const resultA = increaseLuminance(bgRgba, fgRgba, ratio); |
| 304 |
const resultARatio = contrastRatio(bgL, rgb.relativeLuminance(resultA >> 8)); |
| 305 |
if (resultARatio < ratio) { |
| 306 |
const resultB = reduceLuminance(bgRgba, fgRgba, ratio); |
| 307 |
const resultBRatio = contrastRatio(bgL, rgb.relativeLuminance(resultB >> 8)); |
| 308 |
return resultARatio > resultBRatio ? resultA : resultB; |
| 309 |
} |
| 310 |
return resultA; |
| 311 |
} |
| 312 |
return undefined; |
| 313 |
} |
| 314 |
|
| 315 |
export function reduceLuminance(bgRgba: number, fgRgba: number, ratio: number): number { |
| 316 |
// This is a naive but fast approach to reducing luminance as converting to |
| 317 |
// HSL and back is expensive |
| 318 |
const bgR = (bgRgba >> 24) & 0xFF; |
| 319 |
const bgG = (bgRgba >> 16) & 0xFF; |
| 320 |
const bgB = (bgRgba >> 8) & 0xFF; |
| 321 |
let fgR = (fgRgba >> 24) & 0xFF; |
| 322 |
let fgG = (fgRgba >> 16) & 0xFF; |
| 323 |
let fgB = (fgRgba >> 8) & 0xFF; |
| 324 |
let cr = contrastRatio(rgb.relativeLuminance2(fgR, fgG, fgB), rgb.relativeLuminance2(bgR, bgG, bgB)); |
| 325 |
while (cr < ratio && (fgR > 0 || fgG > 0 || fgB > 0)) { |
| 326 |
// Reduce by 10% until the ratio is hit |
| 327 |
fgR -= Math.max(0, Math.ceil(fgR * 0.1)); |
| 328 |
fgG -= Math.max(0, Math.ceil(fgG * 0.1)); |
| 329 |
fgB -= Math.max(0, Math.ceil(fgB * 0.1)); |
| 330 |
cr = contrastRatio(rgb.relativeLuminance2(fgR, fgG, fgB), rgb.relativeLuminance2(bgR, bgG, bgB)); |
| 331 |
} |
| 332 |
return (fgR << 24 | fgG << 16 | fgB << 8 | 0xFF) >>> 0; |
| 333 |
} |
| 334 |
|
| 335 |
export function increaseLuminance(bgRgba: number, fgRgba: number, ratio: number): number { |
| 336 |
// This is a naive but fast approach to increasing luminance as converting to |
| 337 |
// HSL and back is expensive |
| 338 |
const bgR = (bgRgba >> 24) & 0xFF; |
| 339 |
const bgG = (bgRgba >> 16) & 0xFF; |
| 340 |
const bgB = (bgRgba >> 8) & 0xFF; |
| 341 |
let fgR = (fgRgba >> 24) & 0xFF; |
| 342 |
let fgG = (fgRgba >> 16) & 0xFF; |
| 343 |
let fgB = (fgRgba >> 8) & 0xFF; |
| 344 |
let cr = contrastRatio(rgb.relativeLuminance2(fgR, fgG, fgB), rgb.relativeLuminance2(bgR, bgG, bgB)); |
| 345 |
while (cr < ratio && (fgR < 0xFF || fgG < 0xFF || fgB < 0xFF)) { |
| 346 |
// Increase by 10% until the ratio is hit |
| 347 |
fgR = Math.min(0xFF, fgR + Math.ceil((255 - fgR) * 0.1)); |
| 348 |
fgG = Math.min(0xFF, fgG + Math.ceil((255 - fgG) * 0.1)); |
| 349 |
fgB = Math.min(0xFF, fgB + Math.ceil((255 - fgB) * 0.1)); |
| 350 |
cr = contrastRatio(rgb.relativeLuminance2(fgR, fgG, fgB), rgb.relativeLuminance2(bgR, bgG, bgB)); |
| 351 |
} |
| 352 |
return (fgR << 24 | fgG << 16 | fgB << 8 | 0xFF) >>> 0; |
| 353 |
} |
| 354 |
|
| 355 |
export function toChannels(value: number): [number, number, number, number] { |
| 356 |
return [(value >> 24) & 0xFF, (value >> 16) & 0xFF, (value >> 8) & 0xFF, value & 0xFF]; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
export function toPaddedHex(c: number): string { |
| 361 |
const s = c.toString(16); |
| 362 |
return s.length < 2 ? '0' + s : s; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Gets the contrast ratio between two relative luminance values. |
| 367 |
* @param l1 The first relative luminance. |
| 368 |
* @param l2 The first relative luminance. |
| 369 |
* @see https://www.w3.org/TR/WCAG20/#contrast-ratiodef |
| 370 |
*/ |
| 371 |
export function contrastRatio(l1: number, l2: number): number { |
| 372 |
if (l1 < l2) { |
| 373 |
return (l2 + 0.05) / (l1 + 0.05); |
| 374 |
} |
| 375 |
return (l1 + 0.05) / (l2 + 0.05); |
| 376 |
} |