364
return new Color(new RGBA(fgR, fgG, fgB));
365
}
367
>
private static _relativeLuminanceForComponent(color: number): number {
368
const c = color / 255;
369
return (c <= 0.03928) ? c / 12.92 : Math.pow(((c + 0.055) / 1.055), 2.4);
370
}
372
>
/**
373
>
* http://www.w3.org/TR/WCAG20/#contrast-ratiodef
374
>
* Returns the contrast ration number in the set [1, 21].
375
>
*/
376
>
getContrastRatio(another: Color): number {
377
const lum1 = this.getRelativeLuminance();
378
const lum2 = another.getRelativeLuminance();
379
return lum1 > lum2 ? (lum1 + 0.05) / (lum2 + 0.05) : (lum2 + 0.05) / (lum1 + 0.05);
380
}
382
>
/**
383
>
* http://24ways.org/2010/calculating-color-contrast
384
>
* Return 'true' if darker color otherwise 'false'
385
>
*/
386
>
isDarker(): boolean {
387
const yiq = (this.rgba.r * 299 + this.rgba.g * 587 + this.rgba.b * 114) / 1000;
388
return yiq < 128;
389
}
391
>
/**
392
>
* http://24ways.org/2010/calculating-color-contrast
393
>
* Return 'true' if lighter color otherwise 'false'
394
>
*/
395
>
isLighter(): boolean {
396
const yiq = (this.rgba.r * 299 + this.rgba.g * 587 + this.rgba.b * 114) / 1000;
397
return yiq >= 128;
398
}
400
>
isLighterThan(another: Color): boolean {
401
const lum1 = this.getRelativeLuminance();
402
const lum2 = another.getRelativeLuminance();
403
return lum1 > lum2;
404
}
406
>
isDarkerThan(another: Color): boolean {
407
const lum1 = this.getRelativeLuminance();
408
const lum2 = another.getRelativeLuminance();
409
return lum1 < lum2;
410
}
412
>
/**
413
>
* Based on xterm.js: https://github.com/xtermjs/xterm.js/blob/44f9fa39ae03e2ca6d28354d88a399608686770e/src/common/Color.ts#L288
414
>
*
415
>
* Given a foreground color and a background color, either increase or reduce the luminance of the
416
>
* foreground color until the specified contrast ratio is met. If pure white or black is hit
417
>
* without the contrast ratio being met, go the other direction using the background color as the
418
>
* foreground color and take either the first or second result depending on which has the higher
419
>
* contrast ratio.
420
>
*
421
>
* @param foreground The foreground color.
422
>
* @param ratio The contrast ratio to achieve.
423
>
* @returns The adjusted foreground color.
424
>
*/
425
>
ensureConstrast(foreground: Color, ratio: number): Color {
426
const bgL = this.getRelativeLuminance();
427
const fgL = foreground.getRelativeLuminance();