-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
rgb.ts
45 lines (39 loc) 路 1.35 KB
/
rgb.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { InputObject, RgbaColor } from "../types";
import { ALPHA_PRECISION } from "../constants";
import { round, clamp, isPresent } from "../helpers";
export const clampRgba = (rgba: RgbaColor): RgbaColor => ({
r: clamp(rgba.r, 0, 255),
g: clamp(rgba.g, 0, 255),
b: clamp(rgba.b, 0, 255),
a: clamp(rgba.a),
});
export const roundRgba = (rgba: RgbaColor): RgbaColor => ({
r: round(rgba.r),
g: round(rgba.g),
b: round(rgba.b),
a: round(rgba.a, ALPHA_PRECISION),
});
export const parseRgba = ({ r, g, b, a = 1 }: InputObject): RgbaColor | null => {
if (!isPresent(r) || !isPresent(g) || !isPresent(b)) return null;
return clampRgba({
r: Number(r),
g: Number(g),
b: Number(b),
a: Number(a),
});
};
/**
* Converts an RGB channel [0-255] to its linear light (un-companded) form [0-1].
* Linearized RGB values are widely used for color space conversions and contrast calculations
*/
export const linearizeRgbChannel = (value: number): number => {
const ratio = value / 255;
return ratio < 0.04045 ? ratio / 12.92 : Math.pow((ratio + 0.055) / 1.055, 2.4);
};
/**
* Converts an linear-light sRGB channel [0-1] back to its gamma corrected form [0-255]
*/
export const unlinearizeRgbChannel = (ratio: number): number => {
const value = ratio > 0.0031308 ? 1.055 * Math.pow(ratio, 1 / 2.4) - 0.055 : 12.92 * ratio;
return value * 255;
};