Skip to content

Commit

Permalink
Add gamutSpace to ColorSpace
Browse files Browse the repository at this point in the history
Some colorspaces such as hsluv, okhsl and okhsv need to do
gamut checking in a color space that is not their own space or
their base space. For example hsluv needs to do gamut checking in
the srgb color space.

A gamutSpace can now be specified when constructing a ColorSpace and
a gamutSpace property has beed added to ColorSpace.

If a gamutSpace is not specified the following rules are used to
assign the gamutSpace property:

- if the color space is polar use the base space
- if the color space is not polar use the current space

Polar spaces that need to gamut check themselves (hpluv) can set the
gamutSpace option to "self" to avoid gamut checking using the base
space.
  • Loading branch information
lloydk committed Jan 23, 2024
1 parent 43b2176 commit 795a251
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
32 changes: 27 additions & 5 deletions src/space.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ export default class ColorSpace {
this.formats.color.id = this.id;
}

// Gamut space

if (options.gamutSpace) {
// Gamut space explicitly specified
this.gamutSpace = options.gamutSpace === "self" ? this : ColorSpace.get(options.gamutSpace);
}
else {
// No gamut space specified, calculate a sensible default
if (this.isPolar) {
// Do not check gamut through polar coordinates
this.gamutSpace = this.base;
}
else {
this.gamutSpace = this;
}
}

// Optimize inGamut for unbounded spaces
if (this.gamutSpace.isUnbounded) {
this.inGamut = (coords, options) => {
return true;
};
}

// Other stuff
this.referred = options.referred;

Expand All @@ -68,11 +92,9 @@ export default class ColorSpace {
}

inGamut (coords, {epsilon = ε} = {}) {
if (this.isPolar) {
// Do not check gamut through polar coordinates
coords = this.toBase(coords);

return this.base.inGamut(coords, {epsilon});
if (!this.equals(this.gamutSpace)) {
coords = this.to(this.gamutSpace, coords);
return this.gamutSpace.inGamut(coords, {epsilon});
}

let coordMeta = Object.values(this.coords);
Expand Down
2 changes: 2 additions & 0 deletions types/src/space.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface Options {
cssId?: string | undefined;
referred?: string | undefined;
formats?: Record<string, Format> | undefined;
gamutSpace?: "self" | string | ColorSpace | null | undefined;
}

export type Ref =
Expand Down Expand Up @@ -91,6 +92,7 @@ export default class ColorSpace {
formats: Record<string, Format>;
referred?: string | undefined;
white: White;
gamutSpace: ColorSpace;

from (color: Color | ColorObject): Coords;
from (space: string | ColorSpace, coords: Coords): Coords;
Expand Down

0 comments on commit 795a251

Please sign in to comment.