Skip to content

Commit

Permalink
feat(color): add lab D50/65 conv, update HSx<>CSS conv
Browse files Browse the repository at this point in the history
- add labLabD50_65/D65_50()
- add lab65 -> CSS conversion (always as D50)
- since there's no official requirement, allow direct conversions
  from sRGB to various HSx modes (also to be backward compatible)
  • Loading branch information
postspectacular committed Jan 31, 2021
1 parent 4c846d6 commit 014e41d
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/color/src/css/css.ts
Expand Up @@ -6,6 +6,7 @@ import { hslCss } from "../hsl/hsl-css";
import { hsvCss } from "../hsv/hsv-css";
import { int32Css } from "../int/int-css";
import { labCss } from "../lab/lab-css";
import { labLabD65_50 } from "../lab/lab-lab";
import { lchCss } from "../lch/lch-css";
import { rgbCss } from "../rgb/rgb-css";
import { srgbCss } from "../srgb/srgb-css";
Expand All @@ -14,7 +15,7 @@ const CSS_CONVERSIONS: Partial<Record<ColorMode, Fn<any, string>>> = {
hsl: hslCss,
hsv: hsvCss,
lab50: labCss,
lab65: labCss,
lab65: (x) => labCss(labLabD65_50([], x)),
lch: lchCss,
rgb: rgbCss,
srgb: srgbCss,
Expand Down
2 changes: 1 addition & 1 deletion packages/color/src/hcy/hcy.ts
Expand Up @@ -30,5 +30,5 @@ export declare class HCY implements TypedColor<HCY> {
export const hcy = <ColorFactory<HCY>>defColor({
mode: "hcy",
order: <const>["h", "c", "y", "alpha"],
from: { rgb: rgbHcy },
from: { rgb: rgbHcy, srgb: rgbHcy },
});
2 changes: 1 addition & 1 deletion packages/color/src/hsi/hsi.ts
Expand Up @@ -30,5 +30,5 @@ export declare class HSI implements TypedColor<HSI> {
export const hsi = <ColorFactory<HSI>>defColor({
mode: "hsi",
order: <const>["h", "s", "i", "alpha"],
from: { rgb: rgbHsi },
from: { rgb: rgbHsi, srgb: rgbHsi },
});
2 changes: 1 addition & 1 deletion packages/color/src/hsl/hsl.ts
Expand Up @@ -62,5 +62,5 @@ export declare class HSL implements TypedColor<HSL> {
export const hsl = <ColorFactory<HSL>>defColor({
mode: "hsl",
order: <const>["h", "s", "l", "alpha"],
from: { rgb: rgbHsl, hsv: hsvHsl },
from: { rgb: rgbHsl, srgb: rgbHsl, hsv: hsvHsl },
});
2 changes: 1 addition & 1 deletion packages/color/src/hsv/hsv.ts
Expand Up @@ -62,5 +62,5 @@ export declare class HSV implements TypedColor<HSV> {
export const hsv = <ColorFactory<HSV>>defColor({
mode: "hsv",
order: <const>["h", "s", "v", "alpha"],
from: { rgb: rgbHsv, hsl: hslHsv },
from: { rgb: rgbHsv, srgb: rgbHsv, hsl: hslHsv },
});
4 changes: 3 additions & 1 deletion packages/color/src/index.ts
@@ -1,6 +1,8 @@
export * from "./api";
export * from "./api/constants";
export * from "./api/gradients";
export * from "./api/names";
export * from "./api/ranges";
export * from "./api/system";

export * from "./color";
Expand All @@ -27,7 +29,7 @@ export * from "./hsv/hsv-rgb";
export * from "./hsv/hsv";

export * from "./int/int-css";
export * from "./int/int-rgba";
export * from "./int/int-rgb";
export * from "./int/int-srgb";

export * from "./lab/lab-css";
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions packages/color/src/lab/lab-lab.ts
@@ -0,0 +1,10 @@
import type { ColorOp } from "../api";
import { xyzLab, xyzLabD65 } from "../xyz/xyz-lab";
import { xyzXyzD50_65, xyzXyzD65_50 } from "../xyz/xyz-xyz";
import { labXyz, labXyzD65 } from "./lab-xyz";

export const labLabD50_65: ColorOp = (out, src) =>
xyzLabD65(out, xyzXyzD50_65(out, labXyz(out, src)));

export const labLabD65_50: ColorOp = (out, src) =>
xyzLab(out, xyzXyzD65_50(out, labXyzD65(out, src)));
2 changes: 2 additions & 0 deletions packages/color/src/lab/lab50.ts
Expand Up @@ -5,6 +5,7 @@ import { defColor } from "../defcolor";
import { rgbLab } from "../rgb/rgb-lab";
import { xyzLab } from "../xyz/xyz-lab";
import { xyzXyzD65_50 } from "../xyz/xyz-xyz";
import { labLabD65_50 } from "./lab-lab";
import { lchLab } from "./lab-lch";

export declare class LabD50 implements TypedColor<LabD50> {
Expand Down Expand Up @@ -40,6 +41,7 @@ export const labD50 = <ColorFactory<LabD50>>defColor({
from: {
rgb: rgbLab,
lch: lchLab,
lab65: labLabD65_50,
xyz50: xyzLab,
xyz65: [xyzXyzD65_50, xyzLab],
},
Expand Down
2 changes: 2 additions & 0 deletions packages/color/src/lab/lab65.ts
Expand Up @@ -5,6 +5,7 @@ import { defColor } from "../defcolor";
import { rgbLabD65 } from "../rgb/rgb-lab";
import { xyzLabD65 } from "../xyz/xyz-lab";
import { xyzXyzD50_65 } from "../xyz/xyz-xyz";
import { labLabD50_65 } from "./lab-lab";
import { lchLab } from "./lab-lch";

export declare class LabD65 implements TypedColor<LabD65> {
Expand Down Expand Up @@ -40,6 +41,7 @@ export const labD65 = <ColorFactory<LabD65>>defColor({
from: {
rgb: rgbLabD65,
lch: lchLab,
lab50: labLabD50_65,
xyz50: [xyzXyzD50_65, xyzLabD65],
xyz65: xyzLabD65,
},
Expand Down
4 changes: 1 addition & 3 deletions packages/color/src/rgb/rgb-hsi.ts
Expand Up @@ -9,9 +9,7 @@ const SQRT32 = SQRT3 / 2;

export const rgbHsi: ColorOp = (out, src) => {
out = clamp(out || src, src);
const r = out[0];
const g = out[1];
const b = out[2];
const { 0: r, 1: g, 2: b } = out;
const i = THIRD * (r + g + b);
return i < 1e-6 || (r === g && r === b)
? setC3(out, 0, 0, i)
Expand Down

0 comments on commit 014e41d

Please sign in to comment.