Skip to content

Commit

Permalink
feat(color): add Hue enum, closestHue*() fns, namedHueRgba()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Dec 29, 2018
1 parent 172f507 commit e7bb46b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
23 changes: 19 additions & 4 deletions packages/color/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,25 @@ export const YELLOW = Object.freeze([1, 1, 0, 1]);

export const RGB_LUMINANCE = [0.299, 0.587, 0.114];

export const SRGB_GAMMA = 1.0 / 2.2;
export const SRGB_INVERSE_GAMMA = 2.2;
// Hue names

export enum Hue {
RED,
ORANGE,
YELLOW,
CHARTREUSE,
GREEN,
SPRING_GREEN,
CYAN,
AZURE,
BLUE,
VIOLET,
MAGENTA,
ROSE
}

// internal helpers

export const SRGB_ALPHA = 0.055;

export const RGB_XYZ = [
Expand All @@ -69,8 +86,6 @@ export const XYZ_RGB = [
0.0556434, -0.2040259, 1.0572252
];

// internal helpers

export const FF = float(2);
export const PC = percent(2);
export const INV8BIT = 1 / 0xff;
20 changes: 20 additions & 0 deletions packages/color/src/closest-hue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Hue } from "./api";
import { ensureHue } from "./internal/ensure-hue";

/**
* Returns the `Hue` constant of the closest of 12 defined hues.
*
* @param h hue
*/
export const closestHue =
(h: number): Hue =>
Math.round(ensureHue(h) * 12) % 12;

/**
* Returns the `Hue` constant of the closest primary or secondary hue.
*
* @param h
*/
export const closestPrimaryHue =
(h: number): Hue =>
(Math.round(ensureHue(h) * 12) % 12) & 0xe;
6 changes: 5 additions & 1 deletion packages/color/src/hue-rgba.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { clamp01 } from "@thi.ng/math/interval";
import { setC4 } from "@thi.ng/vectors3/setc";
import { Color } from "./api";
import { Color, Hue } from "./api";
import { ensureHue } from "./internal/ensure-hue";

/**
Expand All @@ -21,3 +21,7 @@ export const hueRgba =
alpha
);
};

export const namedHueRgba =
(out: Color, hue: Hue, alpha = 1) =>
hueRgba(out, hue / 12, alpha);

0 comments on commit e7bb46b

Please sign in to comment.