Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gamutSpace to ColorSpace #369

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
LeaVerou marked this conversation as resolved.
Show resolved Hide resolved
}
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