| 1 |
/** |
| 2 |
* @packageDocumentation |
| 3 |
* Defines the class and other types related to creating color spaces. |
| 4 |
* For the builtin color spaces, see the `spaces` module. |
| 5 |
*/ |
| 6 |
import { type, isNone, isInstance } from "./util.js"; |
| 7 |
import Format from "./Format.js"; |
| 8 |
import { getWhite } from "./adapt.js"; |
| 9 |
import hooks from "./hooks.js"; |
| 10 |
import getColor from "./getColor.js"; |
| 11 |
|
| 12 |
const ε = 0.000075; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class to represent a color space |
| 16 |
*/ |
| 17 |
export default class ColorSpace { |
| 18 |
constructor (options) { |
| 19 |
this.id = options.id; |
| 20 |
this.name = options.name; |
| 21 |
this.base = options.base ? ColorSpace.get(options.base) : null; |
| 22 |
this.aliases = options.aliases; |
| 23 |
this.displaySpaces = options.displaySpaces?.map(space => ColorSpace.get(space)); |
| 24 |
|
| 25 |
// This space's ancestors, closest first (immediate base → root), excluding the space itself. |
| 26 |
this.bases = []; |
| 27 |
for (let base = this.base; base; base = base.base) { |
| 28 |
this.bases.push(base); |
| 29 |
} |
| 30 |
|
| 31 |
if (options.rgbGamut) { |
| 32 |
this.rgbGamut = options.rgbGamut; |
| 33 |
} |
| 34 |
|
| 35 |
if (options.linearGamut) { |
| 36 |
this.linearGamut = options.linearGamut; |
| 37 |
} |
| 38 |
|
| 39 |
if (this.base) { |
| 40 |
// Keep a subclass's own methods if the options don't override them |
| 41 |
if (options.fromBase) { |
| 42 |
this.fromBase = options.fromBase; |
| 43 |
} |
| 44 |
|
| 45 |
if (options.toBase) { |
| 46 |
this.toBase = options.toBase; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Coordinate metadata |
| 51 |
|
| 52 |
let coords = options.coords ?? this.base.coords; |
| 53 |
|
| 54 |
for (let name in coords) { |
| 55 |
if (!("name" in coords[name])) { |
| 56 |
coords[name].name = name; |
| 57 |
} |
| 58 |
} |
| 59 |
this.coords = coords; |
| 60 |
|
| 61 |
// White point |
| 62 |
|
| 63 |
let white = options.white ?? this.base.white ?? "D65"; |
| 64 |
this.white = getWhite(white); |
| 65 |
|
| 66 |
// Sort out formats |
| 67 |
|
| 68 |
this.formats = options.formats ?? {}; |
| 69 |
|
| 70 |
for (let name in this.formats) { |
| 71 |
let format = this.formats[name]; |
| 72 |
format.type ||= "function"; |
| 73 |
format.name ||= name; |
| 74 |
} |
| 75 |
|
| 76 |
if (!this.formats.color?.id) { |
| 77 |
this.formats.color = { |
| 78 |
...(this.formats.color ?? {}), |
| 79 |
id: options.cssId || this.id, |
| 80 |
}; |
| 81 |
} |
| 82 |
|
| 83 |
// Gamut space |
| 84 |
|
| 85 |
if (options.gamutSpace) { |
| 86 |
// Gamut space explicitly specified |
| 87 |
this.gamutSpace = |
| 88 |
options.gamutSpace === "self" ? this : ColorSpace.get(options.gamutSpace); |
| 89 |
} |
| 90 |
else { |
| 91 |
// No gamut space specified, calculate a sensible default |
| 92 |
if (this.isPolar) { |
| 93 |
// Do not check gamut through polar coordinates |
| 94 |
this.gamutSpace = this.base; |
| 95 |
} |
| 96 |
else { |
| 97 |
this.gamutSpace = this; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Optimize inGamut for unbounded spaces |
| 102 |
if (this.gamutSpace.isUnbounded) { |
| 103 |
this.inGamut = (coords, options) => { |
| 104 |
return true; |
| 105 |
}; |
| 106 |
} |
| 107 |
|
| 108 |
// Matrices used by this color space, exposed so that consumer code |
| 109 |
// can reuse them instead of duplicating the data. E.g. `this.M.toXYZ`. |
| 110 |
this.M = options.M ?? {}; |
| 111 |
|
| 112 |
// Other stuff |
| 113 |
this.referred = options.referred; |
| 114 |
|
| 115 |
// Compute ancestors and store them, since they will never change |
| 116 |
Object.defineProperty(this, "path", { |
| 117 |
value: getPath(this).reverse(), |
| 118 |
writable: false, |
| 119 |
enumerable: true, |
| 120 |
configurable: true, |
| 121 |
}); |
| 122 |
|
| 123 |
hooks.run("colorspace-init-end", this); |
| 124 |
} |
| 125 |
|
| 126 |
inGamut (coords, { epsilon = ε } = {}) { |
| 127 |
if (!this.equals(this.gamutSpace)) { |
| 128 |
coords = this.to(this.gamutSpace, coords); |
| 129 |
return this.gamutSpace.inGamut(coords, { epsilon }); |
| 130 |
} |
| 131 |
|
| 132 |
let coordMeta = Object.values(this.coords); |
| 133 |
|
| 134 |
return coords.every((c, i) => { |
| 135 |
let meta = coordMeta[i]; |
| 136 |
|
| 137 |
if (meta.type !== "angle" && meta.range) { |
| 138 |
if (isNone(c)) { |
| 139 |
// NaN is always in gamut |
| 140 |
return true; |
| 141 |
} |
| 142 |
|
| 143 |
let [min, max] = meta.range; |
| 144 |
return ( |
| 145 |
(min === undefined || c >= min - epsilon) && |
| 146 |
(max === undefined || c <= max + epsilon) |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
return true; |
| 151 |
}); |
| 152 |
} |
| 153 |
|
| 154 |
get isUnbounded () { |
| 155 |
return Object.values(this.coords).every(coord => !("range" in coord)); |
| 156 |
} |
| 157 |
|
| 158 |
get cssId () { |
| 159 |
return this.formats?.color?.id || this.id; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* The id of this space's hue coordinate, or null if the space is not polar |
| 164 |
* @returns {string | null} |
| 165 |
*/ |
| 166 |
get hueId () { |
| 167 |
for (let id in this.coords) { |
| 168 |
if (this.coords[id].type === "angle") { |
| 169 |
return id; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return null; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* The index of this space's hue coordinate within its coords array, or -1 if the space is not polar |
| 178 |
* @returns {number} |
| 179 |
*/ |
| 180 |
get hueIndex () { |
| 181 |
return Object.keys(this.coords).findIndex(id => this.coords[id].type === "angle"); |
| 182 |
} |
| 183 |
|
| 184 |
get isPolar () { |
| 185 |
return this.hueId !== null; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Lookup a format in this color space |
| 190 |
* @param {string | object | Format} format - Format id if string. If object, it's converted to a `Format` object and returned. |
| 191 |
* @returns {Format} |
| 192 |
*/ |
| 193 |
getFormat (format) { |
| 194 |
if (!format) { |
| 195 |
return null; |
| 196 |
} |
| 197 |
|
| 198 |
if (format === "default") { |
| 199 |
format = Object.values(this.formats)[0]; |
| 200 |
} |
| 201 |
else if (typeof format === "string") { |
| 202 |
format = this.formats[format]; |
| 203 |
} |
| 204 |
|
| 205 |
let ret = Format.get(format, this); |
| 206 |
|
| 207 |
if (ret !== format && format.name in this.formats) { |
| 208 |
// Update the format we have on file so we can find it more quickly next time |
| 209 |
this.formats[format.name] = ret; |
| 210 |
} |
| 211 |
|
| 212 |
return ret; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Check if this color space is the same as another color space reference. |
| 217 |
* Allows proxying color space objects and comparing color spaces with ids. |
| 218 |
* @param {string | ColorSpace} space ColorSpace object or id to compare to |
| 219 |
* @returns {boolean} |
| 220 |
*/ |
| 221 |
equals (space) { |
| 222 |
if (!space) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
|
| 226 |
return this === space || this.id === space || this.id === space.id; |
| 227 |
} |
| 228 |
|
| 229 |
to (space, coords) { |
| 230 |
if (arguments.length === 1) { |
| 231 |
const color = getColor(space); |
| 232 |
[space, coords] = [color.space, color.coords]; |
| 233 |
} |
| 234 |
|
| 235 |
space = ColorSpace.get(space); |
| 236 |
|
| 237 |
if (this.equals(space)) { |
| 238 |
// Same space, no change needed |
| 239 |
return coords; |
| 240 |
} |
| 241 |
|
| 242 |
// Convert NaN to 0, which seems to be valid in every coordinate of every color space |
| 243 |
coords = coords.map(c => (isNone(c) ? 0 : c)); |
| 244 |
|
| 245 |
// Find connection space = lowest common ancestor in the base tree |
| 246 |
let myPath = this.path; |
| 247 |
let otherPath = space.path; |
| 248 |
|
| 249 |
let connectionSpace, connectionSpaceIndex; |
| 250 |
|
| 251 |
for (let i = 0; i < myPath.length; i++) { |
| 252 |
if (myPath[i].equals(otherPath[i])) { |
| 253 |
connectionSpace = myPath[i]; |
| 254 |
connectionSpaceIndex = i; |
| 255 |
} |
| 256 |
else { |
| 257 |
break; |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if (!connectionSpace) { |
| 262 |
// This should never happen |
| 263 |
throw new Error( |
| 264 |
`Cannot convert between color spaces ${this} and ${space}: no connection space was found`, |
| 265 |
); |
| 266 |
} |
| 267 |
|
| 268 |
// Go up from current space to connection space |
| 269 |
for (let i = myPath.length - 1; i > connectionSpaceIndex; i--) { |
| 270 |
coords = myPath[i].toBase(coords); |
| 271 |
} |
| 272 |
|
| 273 |
// Go down from connection space to target space |
| 274 |
for (let i = connectionSpaceIndex + 1; i < otherPath.length; i++) { |
| 275 |
coords = otherPath[i].fromBase(coords); |
| 276 |
} |
| 277 |
|
| 278 |
return coords; |
| 279 |
} |
| 280 |
|
| 281 |
from (space, coords) { |
| 282 |
if (arguments.length === 1) { |
| 283 |
const color = getColor(space); |
| 284 |
[space, coords] = [color.space, color.coords]; |
| 285 |
} |
| 286 |
|
| 287 |
space = ColorSpace.get(space); |
| 288 |
|
| 289 |
return space.to(this, coords); |
| 290 |
} |
| 291 |
|
| 292 |
toString () { |
| 293 |
return `${this.name} (${this.id})`; |
| 294 |
} |
| 295 |
|
| 296 |
getMinCoords () { |
| 297 |
let ret = []; |
| 298 |
|
| 299 |
for (let id in this.coords) { |
| 300 |
let meta = this.coords[id]; |
| 301 |
let range = meta.range || meta.refRange; |
| 302 |
ret.push(range?.min ?? 0); |
| 303 |
} |
| 304 |
|
| 305 |
return ret; |
| 306 |
} |
| 307 |
|
| 308 |
static registry = {}; |
| 309 |
|
| 310 |
// Returns array of unique color spaces |
| 311 |
static get all () { |
| 312 |
return [...new Set(Object.values(ColorSpace.registry))]; |
| 313 |
} |
| 314 |
|
| 315 |
static register (id, space) { |
| 316 |
if (arguments.length === 1) { |
| 317 |
space = arguments[0]; |
| 318 |
id = space.id; |
| 319 |
} |
| 320 |
|
| 321 |
space = this.get(space); |
| 322 |
|
| 323 |
if (this.registry[id] && this.registry[id] !== space) { |
| 324 |
throw new Error(`Duplicate color space registration: '${id}'`); |
| 325 |
} |
| 326 |
this.registry[id] = space; |
| 327 |
|
| 328 |
// Register aliases when called without an explicit ID. |
| 329 |
if (arguments.length === 1 && space.aliases) { |
| 330 |
for (let alias of space.aliases) { |
| 331 |
this.register(alias, space); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
return space; |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Lookup ColorSpace object by name |
| 340 |
* @param {ColorSpace | string} name |
| 341 |
*/ |
| 342 |
static get (space, ...alternatives) { |
| 343 |
if (!space || isInstance(space, this)) { |
| 344 |
return space; |
| 345 |
} |
| 346 |
|
| 347 |
let argType = type(space); |
| 348 |
|
| 349 |
if (argType === "string") { |
| 350 |
// It's a color space id |
| 351 |
let ret = ColorSpace.registry[space.toLowerCase()]; |
| 352 |
|
| 353 |
if (!ret) { |
| 354 |
throw new TypeError(`No color space found with id = "${space}"`); |
| 355 |
} |
| 356 |
|
| 357 |
return ret; |
| 358 |
} |
| 359 |
|
| 360 |
if (alternatives.length) { |
| 361 |
return ColorSpace.get(...alternatives); |
| 362 |
} |
| 363 |
|
| 364 |
throw new TypeError(`${space} is not a valid color space`); |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Look up all color spaces for a format that matches certain criteria |
| 369 |
* @param {object | string} filters |
| 370 |
* @param {Array<ColorSpace>} [spaces=ColorSpace.all] |
| 371 |
* @returns {Format | null} |
| 372 |
*/ |
| 373 |
static findFormat (filters, spaces = ColorSpace.all) { |
| 374 |
if (!filters) { |
| 375 |
return null; |
| 376 |
} |
| 377 |
|
| 378 |
if (typeof filters === "string") { |
| 379 |
filters = { name: filters }; |
| 380 |
} |
| 381 |
|
| 382 |
for (let space of spaces) { |
| 383 |
for (let [name, format] of Object.entries(space.formats)) { |
| 384 |
format.name ??= name; |
| 385 |
format.type ??= "function"; |
| 386 |
|
| 387 |
let matches = |
| 388 |
(!filters.name || format.name === filters.name) && |
| 389 |
(!filters.type || format.type === filters.type); |
| 390 |
|
| 391 |
if (filters.id) { |
| 392 |
let ids = format.ids || [format.id]; |
| 393 |
let filterIds = Array.isArray(filters.id) ? filters.id : [filters.id]; |
| 394 |
matches &&= filterIds.some(id => ids.includes(id)); |
| 395 |
} |
| 396 |
|
| 397 |
if (matches) { |
| 398 |
let ret = Format.get(format, space); |
| 399 |
|
| 400 |
if (ret !== format) { |
| 401 |
space.formats[format.name] = ret; |
| 402 |
} |
| 403 |
|
| 404 |
return ret; |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
return null; |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Get metadata about a coordinate of a color space |
| 414 |
* |
| 415 |
* @static |
| 416 |
* @param {Array | string} ref |
| 417 |
* @param {ColorSpace | string} [workingSpace] |
| 418 |
* @return {Object} |
| 419 |
*/ |
| 420 |
static resolveCoord (ref, workingSpace) { |
| 421 |
let coordType = type(ref); |
| 422 |
let space, coord; |
| 423 |
|
| 424 |
if (coordType === "string") { |
| 425 |
if (ref.includes(".")) { |
| 426 |
// Absolute coordinate |
| 427 |
[space, coord] = ref.split("."); |
| 428 |
} |
| 429 |
else { |
| 430 |
// Relative coordinate |
| 431 |
[space, coord] = [, ref]; |
| 432 |
} |
| 433 |
} |
| 434 |
else if (Array.isArray(ref)) { |
| 435 |
[space, coord] = ref; |
| 436 |
} |
| 437 |
else { |
| 438 |
// Object |
| 439 |
space = ref.space; |
| 440 |
coord = ref.coordId; |
| 441 |
} |
| 442 |
|
| 443 |
space = ColorSpace.get(space); |
| 444 |
|
| 445 |
if (!space) { |
| 446 |
space = workingSpace; |
| 447 |
} |
| 448 |
|
| 449 |
if (!space) { |
| 450 |
throw new TypeError( |
| 451 |
`Cannot resolve coordinate reference ${ref}: No color space specified and relative references are not allowed here`, |
| 452 |
); |
| 453 |
} |
| 454 |
|
| 455 |
coordType = type(coord); |
| 456 |
|
| 457 |
if (coordType === "number" || (coordType === "string" && coord >= 0)) { |
| 458 |
// Resolve numerical coord |
| 459 |
let meta = Object.entries(space.coords)[coord]; |
| 460 |
|
| 461 |
if (meta) { |
| 462 |
return { space, id: meta[0], index: coord, ...meta[1] }; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
space = ColorSpace.get(space); |
| 467 |
|
| 468 |
let normalizedCoord = coord.toLowerCase(); |
| 469 |
|
| 470 |
let i = 0; |
| 471 |
for (let id in space.coords) { |
| 472 |
let meta = space.coords[id]; |
| 473 |
|
| 474 |
if ( |
| 475 |
id.toLowerCase() === normalizedCoord || |
| 476 |
meta.name?.toLowerCase() === normalizedCoord |
| 477 |
) { |
| 478 |
return { space, id, index: i, ...meta }; |
| 479 |
} |
| 480 |
|
| 481 |
i++; |
| 482 |
} |
| 483 |
|
| 484 |
throw new TypeError( |
| 485 |
`No "${coord}" coordinate found in ${space.name}. Its coordinates are: ${Object.keys(space.coords).join(", ")}`, |
| 486 |
); |
| 487 |
} |
| 488 |
|
| 489 |
static DEFAULT_FORMAT = { |
| 490 |
type: "functions", |
| 491 |
name: "color", |
| 492 |
}; |
| 493 |
} |
| 494 |
|
| 495 |
function getPath (space) { |
| 496 |
let ret = [space]; |
| 497 |
|
| 498 |
for (let s = space; (s = s.base);) { |
| 499 |
ret.push(s); |
| 500 |
} |
| 501 |
|
| 502 |
return ret; |
| 503 |
} |