| 1 |
// APCA 0.0.98G |
| 2 |
// https://github.com/Myndex/apca-w3 |
| 3 |
// see also https://github.com/w3c/silver/issues/643 |
| 4 |
|
| 5 |
import getColor from "../getColor.js"; |
| 6 |
import to from "../to.js"; |
| 7 |
import { isNone } from "../util.js"; |
| 8 |
|
| 9 |
// exponents |
| 10 |
const normBG = 0.56; |
| 11 |
const normTXT = 0.57; |
| 12 |
const revTXT = 0.62; |
| 13 |
const revBG = 0.65; |
| 14 |
|
| 15 |
// clamps |
| 16 |
const blkThrs = 0.022; |
| 17 |
const blkClmp = 1.414; |
| 18 |
const loClip = 0.1; |
| 19 |
const deltaYmin = 0.0005; |
| 20 |
|
| 21 |
// scalers |
| 22 |
// see https://github.com/w3c/silver/issues/645 |
| 23 |
const scaleBoW = 1.14; |
| 24 |
const loBoWoffset = 0.027; |
| 25 |
const scaleWoB = 1.14; |
| 26 |
const loWoBoffset = 0.027; |
| 27 |
|
| 28 |
function fclamp (Y) { |
| 29 |
if (Y >= blkThrs) { |
| 30 |
return Y; |
| 31 |
} |
| 32 |
return Y + (blkThrs - Y) ** blkClmp; |
| 33 |
} |
| 34 |
|
| 35 |
function linearize (val) { |
| 36 |
let sign = val < 0 ? -1 : 1; |
| 37 |
let abs = Math.abs(val); |
| 38 |
return sign * Math.pow(abs, 2.4); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Not symmetric, requires a foreground (text) color, and a background color |
| 43 |
* @param {import("../types.js").ColorTypes} background |
| 44 |
* @param {import("../types.js").ColorTypes} foreground |
| 45 |
* @returns {number} |
| 46 |
*/ |
| 47 |
export default function contrastAPCA (background, foreground) { |
| 48 |
foreground = getColor(foreground); |
| 49 |
background = getColor(background); |
| 50 |
|
| 51 |
let S; |
| 52 |
let C; |
| 53 |
let Sapc; |
| 54 |
|
| 55 |
// Myndex as-published, assumes sRGB inputs |
| 56 |
let R, G, B; |
| 57 |
|
| 58 |
foreground = to(foreground, "srgb"); |
| 59 |
// Should these be clamped to in-gamut values? |
| 60 |
|
| 61 |
// Calculates "screen luminance" with non-standard simple gamma EOTF |
| 62 |
// weights should be from CSS Color 4, not the ones here which are via Myndex and copied from Lindbloom |
| 63 |
[R, G, B] = foreground.coords.map(c => { |
| 64 |
return isNone(c) ? 0 : c; |
| 65 |
}); |
| 66 |
let lumTxt = linearize(R) * 0.2126729 + linearize(G) * 0.7151522 + linearize(B) * 0.072175; |
| 67 |
|
| 68 |
background = to(background, "srgb"); |
| 69 |
[R, G, B] = background.coords.map(c => { |
| 70 |
return isNone(c) ? 0 : c; |
| 71 |
}); |
| 72 |
let lumBg = linearize(R) * 0.2126729 + linearize(G) * 0.7151522 + linearize(B) * 0.072175; |
| 73 |
|
| 74 |
// toe clamping of very dark values to account for flare |
| 75 |
let Ytxt = fclamp(lumTxt); |
| 76 |
let Ybg = fclamp(lumBg); |
| 77 |
|
| 78 |
// are we "Black on White" (dark on light), or light on dark? |
| 79 |
let BoW = Ybg > Ytxt; |
| 80 |
|
| 81 |
// why is this a delta, when Y is not perceptually uniform? |
| 82 |
// Answer: it is a noise gate, see |
| 83 |
// https://github.com/LeaVerou/color.js/issues/208 |
| 84 |
if (Math.abs(Ybg - Ytxt) < deltaYmin) { |
| 85 |
C = 0; |
| 86 |
} |
| 87 |
else { |
| 88 |
if (BoW) { |
| 89 |
// dark text on light background |
| 90 |
S = Ybg ** normBG - Ytxt ** normTXT; |
| 91 |
C = S * scaleBoW; |
| 92 |
} |
| 93 |
else { |
| 94 |
// light text on dark background |
| 95 |
S = Ybg ** revBG - Ytxt ** revTXT; |
| 96 |
C = S * scaleWoB; |
| 97 |
} |
| 98 |
} |
| 99 |
if (Math.abs(C) < loClip) { |
| 100 |
Sapc = 0; |
| 101 |
} |
| 102 |
else if (C > 0) { |
| 103 |
// not clear whether Woffset is loBoWoffset or loWoBoffset |
| 104 |
// but they have the same value |
| 105 |
Sapc = C - loBoWoffset; |
| 106 |
} |
| 107 |
else { |
| 108 |
Sapc = C + loBoWoffset; |
| 109 |
} |
| 110 |
|
| 111 |
return Sapc * 100; |
| 112 |
} |