-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
colord.ts
168 lines (148 loc) 路 4.72 KB
/
colord.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { Input, AnyColor, RgbaColor, HslaColor, HsvaColor } from "./types";
import { round } from "./helpers";
import { ALPHA_PRECISION } from "./constants";
import { parse } from "./parse";
import { rgbaToHex } from "./colorModels/hex";
import { roundRgba } from "./colorModels/rgb";
import { rgbaToRgbaString } from "./colorModels/rgbString";
import { rgbaToHsla, roundHsla } from "./colorModels/hsl";
import { rgbaToHslaString } from "./colorModels/hslString";
import { rgbaToHsva, roundHsva } from "./colorModels/hsv";
import { changeAlpha } from "./manipulate/changeAlpha";
import { saturate } from "./manipulate/saturate";
import { getBrightness } from "./get/getBrightness";
import { lighten } from "./manipulate/lighten";
import { invert } from "./manipulate/invert";
export class Colord {
private readonly parsed: RgbaColor | null;
readonly rgba: RgbaColor;
constructor(input: AnyColor) {
// Internal color format is RGBA object.
// We do not round the internal RGBA numbers for better conversion accuracy.
this.parsed = parse(input as Input)[0];
this.rgba = this.parsed || { r: 0, g: 0, b: 0, a: 1 };
}
/**
* Returns a boolean indicating whether or not an input has been parsed successfully.
* Note: If parsing is unsuccessful, Colord defaults to black (does not throws an error).
*/
public isValid(): boolean {
return this.parsed !== null;
}
/**
* Returns the brightness of a color (from 0 to 1).
* The calculation logic is modified from WCAG.
* https://www.w3.org/TR/AERT/#color-contrast
*/
public brightness(): number {
return round(getBrightness(this.rgba), 2);
}
/**
* Same as calling `brightness() < 0.5`.
*/
public isDark(): boolean {
return getBrightness(this.rgba) < 0.5;
}
/**
* Same as calling `brightness() >= 0.5`.
* */
public isLight(): boolean {
return getBrightness(this.rgba) >= 0.5;
}
/**
* Returns the hexadecimal representation of a color.
* When the alpha channel value of the color is less than 1,
* it outputs #rrggbbaa format instead of #rrggbb.
*/
public toHex(): string {
return rgbaToHex(this.rgba);
}
/**
* Converts a color to RGB color space and returns an object.
* Always includes an alpha value from 0 to 1.
*/
public toRgb(): RgbaColor {
return roundRgba(this.rgba);
}
/**
* Converts a color to RGB color space and returns a string representation.
* Outputs an alpha value only if it is less than 1.
*/
public toRgbString(): string {
return rgbaToRgbaString(this.rgba);
}
/**
* Converts a color to HSL color space and returns an object.
* Always includes an alpha value from 0 to 1.
*/
public toHsl(): HslaColor {
return roundHsla(rgbaToHsla(this.rgba));
}
/**
* Converts a color to HSL color space and returns a string representation.
* Always includes an alpha value from 0 to 1.
*/
public toHslString(): string {
return rgbaToHslaString(this.rgba);
}
/**
* Converts a color to HSV color space and returns an object.
* Always includes an alpha value from 0 to 1.
*/
public toHsv(): HsvaColor {
return roundHsva(rgbaToHsva(this.rgba));
}
/**
* Creates a new instance containing an inverted (opposite) version of the color.
*/
public invert(): Colord {
return colord(invert(this.rgba));
}
/**
* Increases the HSL saturation of a color by the given amount.
*/
public saturate(amount = 0.1): Colord {
return colord(saturate(this.rgba, amount));
}
/**
* Decreases the HSL saturation of a color by the given amount.
*/
public desaturate(amount = 0.1): Colord {
return colord(saturate(this.rgba, -amount));
}
/**
* Makes a gray color with the same lightness as a source color.
*/
public grayscale(): Colord {
return colord(saturate(this.rgba, -1));
}
/**
* Increases the HSL lightness of a color by the given amount.
*/
public lighten(amount = 0.1): Colord {
return colord(lighten(this.rgba, amount));
}
/**
* Increases the HSL lightness of a color by the given amount.
*/
public darken(amount = 0.1): Colord {
return colord(lighten(this.rgba, -amount));
}
/**
* Allows to get or change an alpha channel value.
*/
public alpha(): number;
public alpha(value: number): Colord;
public alpha(value?: number): Colord | number {
if (typeof value === "number") return colord(changeAlpha(this.rgba, value));
return round(this.rgba.a, ALPHA_PRECISION);
}
}
/**
* Parses the given input color and creates a new `Colord` instance.
* See accepted input formats: https://github.com/omgovich/colord#color-parsing
*/
export const colord = (input: AnyColor | Colord): Colord => {
if (input instanceof Colord) return input;
return new Colord(input);
};