| 1 |
import RGBColorSpace from "../RGBColorSpace.js"; |
| 2 |
import sRGBLinear from "./srgb-linear.js"; |
| 3 |
import KEYWORDS from "../keywords.js"; |
| 4 |
|
| 5 |
/** @import { Coords } from "../types.js" */ |
| 6 |
|
| 7 |
let coordGrammar = Array(3).fill("<percentage> | <number>[0, 255]"); |
| 8 |
let coordGrammarNumber = Array(3).fill("<number>[0, 255]"); |
| 9 |
|
| 10 |
export default new RGBColorSpace({ |
| 11 |
id: "srgb", |
| 12 |
name: "sRGB", |
| 13 |
base: sRGBLinear, |
| 14 |
linearGamut: sRGBLinear, |
| 15 |
fromBase: rgb => { |
| 16 |
// convert an array of linear-light sRGB values in the range 0.0-1.0 |
| 17 |
// to gamma corrected form |
| 18 |
// https://en.wikipedia.org/wiki/SRGB |
| 19 |
return rgb.map(val => { |
| 20 |
let sign = val < 0 ? -1 : 1; |
| 21 |
let abs = val * sign; |
| 22 |
|
| 23 |
if (abs > 0.0031308) { |
| 24 |
return sign * (1.055 * abs ** (1 / 2.4) - 0.055); |
| 25 |
} |
| 26 |
|
| 27 |
return 12.92 * val; |
| 28 |
}); |
| 29 |
}, |
| 30 |
toBase: rgb => { |
| 31 |
// convert an array of sRGB values in the range 0.0 - 1.0 |
| 32 |
// to linear light (un-companded) form. |
| 33 |
// https://en.wikipedia.org/wiki/SRGB |
| 34 |
return rgb.map(val => { |
| 35 |
let sign = val < 0 ? -1 : 1; |
| 36 |
let abs = val * sign; |
| 37 |
|
| 38 |
if (abs <= 0.04045) { |
| 39 |
return val / 12.92; |
| 40 |
} |
| 41 |
|
| 42 |
return sign * ((abs + 0.055) / 1.055) ** 2.4; |
| 43 |
}); |
| 44 |
}, |
| 45 |
formats: { |
| 46 |
rgb: { |
| 47 |
coords: coordGrammar, |
| 48 |
}, |
| 49 |
rgb_number: { |
| 50 |
name: "rgb", |
| 51 |
commas: true, |
| 52 |
coords: coordGrammarNumber, |
| 53 |
alpha: false, |
| 54 |
}, |
| 55 |
color: { |
| 56 |
/* use defaults */ |
| 57 |
}, |
| 58 |
rgba: { |
| 59 |
coords: coordGrammar, |
| 60 |
commas: true, |
| 61 |
alpha: true, |
| 62 |
}, |
| 63 |
rgba_number: { |
| 64 |
name: "rgba", |
| 65 |
commas: true, |
| 66 |
coords: coordGrammarNumber, |
| 67 |
}, |
| 68 |
hex: { |
| 69 |
type: "custom", |
| 70 |
toGamut: true, |
| 71 |
test: str => /^#(([a-f0-9]{2}){3,4}|[a-f0-9]{3,4})$/i.test(str), |
| 72 |
parse (str) { |
| 73 |
if (str.length <= 5) { |
| 74 |
// #rgb or #rgba, duplicate digits |
| 75 |
str = str.replace(/[a-f0-9]/gi, "$&$&"); |
| 76 |
} |
| 77 |
|
| 78 |
/** @type {number[]} */ |
| 79 |
let rgba = []; |
| 80 |
// @ts-expect-error Type 'void' is not assignable to type 'string' |
| 81 |
str.replace(/[a-f0-9]{2}/gi, component => { |
| 82 |
rgba.push(parseInt(component, 16) / 255); |
| 83 |
}); |
| 84 |
|
| 85 |
return { |
| 86 |
spaceId: "srgb", |
| 87 |
coords: /** @type {Coords} */ (rgba.slice(0, 3)), |
| 88 |
alpha: /** @type {number} */ (rgba.slice(3)[0]), |
| 89 |
}; |
| 90 |
}, |
| 91 |
serialize: ( |
| 92 |
coords, |
| 93 |
alpha, |
| 94 |
{ |
| 95 |
collapse = true, // collapse to 3-4 digit hex when possible? |
| 96 |
alpha: alphaFormat, |
| 97 |
} = {}, |
| 98 |
) => { |
| 99 |
if ((alphaFormat !== false && alpha < 1) || alphaFormat === true) { |
| 100 |
coords.push(alpha); |
| 101 |
} |
| 102 |
|
| 103 |
coords = /** @type {[number, number, number]} */ ( |
| 104 |
coords.map(c => Math.round(c * 255)) |
| 105 |
); |
| 106 |
|
| 107 |
let collapsible = collapse && coords.every(c => c % 17 === 0); |
| 108 |
|
| 109 |
let hex = coords |
| 110 |
.map(c => { |
| 111 |
if (collapsible) { |
| 112 |
return (c / 17).toString(16); |
| 113 |
} |
| 114 |
|
| 115 |
return c.toString(16).padStart(2, "0"); |
| 116 |
}) |
| 117 |
.join(""); |
| 118 |
|
| 119 |
return "#" + hex; |
| 120 |
}, |
| 121 |
}, |
| 122 |
keyword: { |
| 123 |
type: "custom", |
| 124 |
test: str => /^[a-z]+$/i.test(str), |
| 125 |
parse (str) { |
| 126 |
str = str.toLowerCase(); |
| 127 |
let ret = { spaceId: "srgb", coords: null, alpha: 1 }; |
| 128 |
|
| 129 |
if (str === "transparent") { |
| 130 |
ret.coords = KEYWORDS.black; |
| 131 |
ret.alpha = 0; |
| 132 |
} |
| 133 |
else { |
| 134 |
ret.coords = KEYWORDS[str]; |
| 135 |
} |
| 136 |
|
| 137 |
if (ret.coords) { |
| 138 |
return ret; |
| 139 |
} |
| 140 |
}, |
| 141 |
}, |
| 142 |
}, |
| 143 |
}); |