Skip to content

Commit

Permalink
feat(color): add TypedColor.xyz 3-channel accessor, update all impls
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 12, 2023
1 parent 39307bf commit c62e0ee
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/color/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ export interface TypedColor<T>
*/
readonly range: [ReadonlyColor, ReadonlyColor];

/**
* Returns first 3 color components as tuple/vector. Not to be confused with
* {@link XYZD50} or {@link XYZD65} color modes.
*/
readonly xyz: [number, number, number];
/**
* Clamps all color channels so that colors is inside RGB gamut.
*
Expand Down
4 changes: 4 additions & 0 deletions packages/color/src/defcolor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ export const defColor = <M extends ColorMode, K extends string>(
return spec.mode;
}

get xyz(): [number, number, number] {
return [this[0], this[1], this[2]];
}

[Symbol.iterator]() {
return stridedValues(
this.buf,
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/hcy/hcy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export declare class HCY implements TypedColor<HCY> {
readonly mode: "hcy";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): HCY;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/hsi/hsi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export declare class HSI implements TypedColor<HSI> {
readonly mode: "hsi";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): HSI;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/hsl/hsl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export declare class HSL implements TypedColor<HSL> {
readonly mode: "hsl";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): HSL;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/hsv/hsv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export declare class HSV implements TypedColor<HSV> {
readonly mode: "hsv";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): HSV;
Expand Down
9 changes: 9 additions & 0 deletions packages/color/src/int/int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ export abstract class Int32<T extends TypedColor<T>> implements TypedColor<T> {
this[0] = (this[0] & 0xffffff) | __scale8bit(x, 24);
}

get xyz(): [number, number, number] {
const val = this[0];
return [
((val >> 16) & 0xff) / 0xff,
((val >> 8) & 0xff) / 0xff,
(val & 0xff) / 0xff,
];
}

*[Symbol.iterator]() {
yield this[0];
}
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/lab/lab50.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export declare class LabD50 implements TypedColor<LabD50> {
readonly mode: "lab50";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): LabD50;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/lab/lab65.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export declare class LabD65 implements TypedColor<LabD65> {
readonly mode: "lab65";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): LabD65;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/lch/lch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export declare class LCH implements TypedColor<LCH> {
readonly mode: "lch";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): LCH;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/oklab/oklab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export declare class Oklab implements TypedColor<Oklab> {
readonly mode: "oklab";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): Oklab;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/oklch/oklch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export declare class Oklch implements TypedColor<Oklch> {
readonly mode: "oklch";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): Oklch;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/rgb/rgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export declare class RGB implements TypedColor<RGB> {
readonly mode: "rgb";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): RGB;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/srgb/srgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export declare class SRGB implements TypedColor<SRGB> {
readonly mode: "srgb";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): SRGB;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/xyy/xyy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export declare class XYY implements TypedColor<XYY> {
readonly mode: "xyy";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): XYY;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/xyz/xyz50.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export declare class XYZD50 implements TypedColor<XYZD50> {
readonly mode: "xyz50";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): XYZD50;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/xyz/xyz65.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export declare class XYZD65 implements TypedColor<XYZD65> {
readonly mode: "xyz65";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): XYZD65;
Expand Down
1 change: 1 addition & 0 deletions packages/color/src/ycc/ycc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export declare class YCC implements TypedColor<YCC> {
readonly mode: "ycc";
readonly length: 4;
readonly range: [ReadonlyColor, ReadonlyColor];
readonly xyz: [number, number, number];
[Symbol.iterator](): Iterator<number, any, undefined>;
clamp(): this;
copy(): YCC;
Expand Down

0 comments on commit c62e0ee

Please sign in to comment.