Skip to content

Commit

Permalink
feat(color): add/update conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 8, 2021
1 parent 8c5f8fb commit e979044
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/color/src/lab/lab65.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const labD65 = <ColorFactory<LabD65>>defColor({
order: <const>["l", "a", "b", "alpha"],
from: {
rgb: rgbLabD65,
lch: lchLab,
lch: [lchLab, labLabD50_65],
lab50: labLabD50_65,
xyz50: [xyzXyzD50_65, xyzLabD65],
xyz65: xyzLabD65,
Expand Down
11 changes: 10 additions & 1 deletion packages/color/src/lch/lch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import type { NumericArray } from "@thi.ng/api";
import type { IRandom } from "@thi.ng/random";
import type { Color, ColorFactory, ReadonlyColor, TypedColor } from "../api";
import { defColor } from "../defcolor";
import { labLabD65_50 } from "../lab/lab-lab";
import { labLch } from "../lab/lab-lch";
import { rgbLab } from "../rgb/rgb-lab";
import { xyzLab } from "../xyz/xyz-lab";
import { xyzXyzD65_50 } from "../xyz/xyz-xyz";

export declare class LCH implements TypedColor<LCH> {
buf: NumericArray;
Expand All @@ -29,6 +32,10 @@ export declare class LCH implements TypedColor<LCH> {
toJSON(): number[];
}

/**
* Luminance Chroma Hue (conversions assume {@link D50} white point, as per CSS
* spec).
*/
export const lch = <ColorFactory<LCH>>defColor({
mode: "lch",
channels: {
Expand All @@ -38,6 +45,8 @@ export const lch = <ColorFactory<LCH>>defColor({
from: {
rgb: (out, src) => labLch(null, rgbLab(out, src)),
lab50: labLch,
lab65: labLch,
lab65: [labLabD65_50, labLch],
xyz50: [xyzLab, labLch],
xyz65: [xyzXyzD65_50, xyzLab, labLch],
},
});
11 changes: 11 additions & 0 deletions packages/color/src/oklab/oklab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { NumericArray } from "@thi.ng/api";
import type { IRandom } from "@thi.ng/random";
import type { Color, ColorFactory, ReadonlyColor, TypedColor } from "../api";
import { defColor } from "../defcolor";
import { lchLab } from "../lab/lab-lch";
import { labXyz, labXyzD65 } from "../lab/lab-xyz";
import { rgbOklab } from "../rgb/rgb-oklab";
import { xyzOklab } from "../xyz/xyz-oklab";
import { xyzXyzD50_65 } from "../xyz/xyz-xyz";
Expand Down Expand Up @@ -30,6 +32,12 @@ export declare class Oklab implements TypedColor<Oklab> {
toJSON(): number[];
}

/**
* Oklab color type. Conversions assume {@link D65} white point).
*
* @remarks
* Reference: https://bottosson.github.io/posts/oklab/
*/
export const oklab = <ColorFactory<Oklab>>defColor({
mode: "oklab",
channels: {
Expand All @@ -38,6 +46,9 @@ export const oklab = <ColorFactory<Oklab>>defColor({
},
order: <const>["l", "a", "b", "alpha"],
from: {
lab50: [labXyz, xyzXyzD50_65, xyzOklab],
lab65: [labXyzD65, xyzOklab],
lch: [lchLab, labXyzD65, xyzOklab],
rgb: rgbOklab,
xyz50: [xyzXyzD50_65, xyzOklab],
xyz65: xyzOklab,
Expand Down
8 changes: 8 additions & 0 deletions packages/color/src/srgb/srgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import type { NumericArray } from "@thi.ng/api";
import type { IRandom } from "@thi.ng/random";
import type { Color, ColorFactory, ReadonlyColor, TypedColor } from "../api";
import { defColor } from "../defcolor";
import { hcyRgb } from "../hcy/hcy-rgb";
import { hsiRgb } from "../hsi/hsi-rgb";
import { hslRgb } from "../hsl/hsl-rgb";
import { hsvRgb } from "../hsv/hsv-rgb";
import { intAbgr32Srgb, intArgb32Srgb } from "../int/int-srgb";
import { rgbSrgb } from "../rgb/rgb-srgb";

Expand Down Expand Up @@ -35,6 +39,10 @@ export const srgb = <ColorFactory<SRGB>>defColor({
from: {
abgr32: (out, src) => intAbgr32Srgb(out, src[0]),
argb32: (out, src) => intArgb32Srgb(out, src[0]),
hcy: hcyRgb,
hsi: hsiRgb,
hsl: hslRgb,
hsv: hsvRgb,
rgb: rgbSrgb,
},
});
2 changes: 2 additions & 0 deletions packages/color/src/xyz/xyz50.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { IRandom } from "@thi.ng/random";
import type { Color, ColorFactory, ReadonlyColor, TypedColor } from "../api";
import { D50 } from "../api/constants";
import { defColor } from "../defcolor";
import { lchLab } from "../lab/lab-lch";
import { labXyz, labXyzD65 } from "../lab/lab-xyz";
import { oklabXyzD65 } from "../oklab/oklab-xyz";
import { rgbXyz } from "../rgb/rgb-xyz";
Expand Down Expand Up @@ -45,6 +46,7 @@ export const xyzD50 = <ColorFactory<XYZD50>>defColor({
rgb: rgbXyz,
lab50: labXyz,
lab65: [labXyzD65, xyzXyzD65_50],
lch: [lchLab, labXyz],
oklab: [oklabXyzD65, xyzXyzD65_50],
xyy: xyyXyz,
},
Expand Down
11 changes: 10 additions & 1 deletion packages/color/src/xyz/xyz65.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { IRandom } from "@thi.ng/random";
import type { Color, ColorFactory, ReadonlyColor, TypedColor } from "../api";
import { D65 } from "../api/constants";
import { defColor } from "../defcolor";
import { labLabD50_65 } from "../lab/lab-lab";
import { lchLab } from "../lab/lab-lch";
import { labXyzD65 } from "../lab/lab-xyz";
import { oklabXyzD65 } from "../oklab/oklab-xyz";
import { rgbXyzD65 } from "../rgb/rgb-xyz";
Expand Down Expand Up @@ -40,5 +42,12 @@ export const xyzD65 = <ColorFactory<XYZD65>>defColor({
z: { range: [0, D65[2]] },
},
order: <const>["x", "y", "z", "alpha"],
from: { rgb: rgbXyzD65, lab65: labXyzD65, oklab: oklabXyzD65, xyy: xyyXyz },
from: {
rgb: rgbXyzD65,
lab50: [labLabD50_65, labXyzD65],
lab65: labXyzD65,
lch: [lchLab, labLabD50_65, labXyzD65],
oklab: oklabXyzD65,
xyy: xyyXyz,
},
});

0 comments on commit e979044

Please sign in to comment.