; } /** * @return {boolean} TRUE if you need to use a bright color (e.g. 'white'), when * this color is set as the background. */ get useBrightText() { return this.relativeLuminance <= CONTRAST_BRIGHTTEXT_THRESHOLD; } /** * Get the contrast ratio between the current color and a second other color. * A common use case is to express the difference between a foreground and a * background color in numbers. * Formula from W3C's WCAG 2.0 spec's contrast ratio, section 1.4.1, * http://www.w3.org/TR/WCAG20/. * * @param {Color} otherColor Color instance to calculate the contrast with * @return {number} Contrast ratios can range from 1 to 21, commonly written * as 1:1 to 21:1. */ contrastRatio(otherColor) { if (!(otherColor instanceof Color)) { throw new TypeError("The first argument should be an instance of Color"); } let luminance = this.relativeLuminance; let otherLuminance = otherColor.relativeLuminance; return ( (Math.max(luminance, otherLuminance) + 0.05) / (Math.min(luminance, otherLuminance) + 0.05) ); } /** * Method to check if the contrast ratio between two colors is high enough to * be discernable. * * @param {Color} otherColor Color instance to calculate the contrast with * @param {string} [level] WCAG conformance level that maps to the minimum * required contrast ratio. Defaults to 'AA' * @return {boolean} */ isContrastRatioAcceptable(otherColor, level = "AA") { return this.contrastRatio(otherColor) > CONTRAST_RATIO_LEVELS[level]; } } PK