vben
authored
5 years ago
1
2
3
4
5
6
7
/**
* 判断是否 十六进制颜色值.
* 输入形式可为 #fff000 #f00
*
* @param String color 十六进制颜色值
* @return Boolean
*/
vben
authored
5 years ago
8
export function isHexColor(color: string) {
9
const reg = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
vben
authored
5 years ago
10
return reg.test(color);
vben
authored
5 years ago
11
}
vben
authored
5 years ago
12
13
14
15
16
17
/**
* RGB 颜色值转换为 十六进制颜色值.
* r, g, 和 b 需要在 [0, 255] 范围内
*
* @return String 类似#ff00ff
vben
authored
5 years ago
18
19
20
* @param r
* @param g
* @param b
vben
authored
5 years ago
21
*/
vben
authored
5 years ago
22
export function rgbToHex(r: number, g: number, b: number) {
vben
authored
5 years ago
23
24
25
// tslint:disable-next-line:no-bitwise
const hex = ((r << 16) | (g << 8) | b).toString(16);
return '#' + new Array(Math.abs(hex.length - 7)).join('0') + hex;
vben
authored
5 years ago
26
}
vben
authored
5 years ago
27
28
29
30
31
32
/**
* Transform a HEX color to its RGB representation
* @param {string} hex The color to transform
* @returns The RGB representation of the passed color
*/
vben
authored
5 years ago
33
export function hexToRGB(hex: string) {
vben
authored
5 years ago
34
35
36
37
38
39
40
41
42
let sHex = hex.toLowerCase();
if (isHexColor(hex)) {
if (sHex.length === 4) {
let sColorNew = '#';
for (let i = 1; i < 4; i += 1) {
sColorNew += sHex.slice(i, i + 1).concat(sHex.slice(i, i + 1));
}
sHex = sColorNew;
}
vben
authored
4 years ago
43
const sColorChange: number[] = [];
vben
authored
5 years ago
44
45
46
47
48
49
for (let i = 1; i < 7; i += 2) {
sColorChange.push(parseInt('0x' + sHex.slice(i, i + 2)));
}
return 'RGB(' + sColorChange.join(',') + ')';
}
return sHex;
vben
authored
5 years ago
50
}
vben
authored
5 years ago
51
vben
authored
5 years ago
52
export function colorIsDark(color: string) {
vben
authored
5 years ago
53
54
55
56
57
58
if (!isHexColor(color)) return;
const [r, g, b] = hexToRGB(color)
.replace(/(?:\(|\)|rgb|RGB)*/g, '')
.split(',')
.map((item) => Number(item));
return r * 0.299 + g * 0.578 + b * 0.114 < 192;
vben
authored
5 years ago
59
}
vben
authored
5 years ago
60
61
62
63
64
65
66
/**
* Darkens a HEX color given the passed percentage
* @param {string} color The color to process
* @param {number} amount The amount to change the color by
* @returns {string} The HEX representation of the processed color
*/
vben
authored
5 years ago
67
export function darken(color: string, amount: number) {
vben
authored
5 years ago
68
69
70
71
color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color;
amount = Math.trunc((255 * amount) / 100);
return `#${subtractLight(color.substring(0, 2), amount)}${subtractLight(
color.substring(2, 4),
vben
authored
4 years ago
72
amount,
vben
authored
5 years ago
73
)}${subtractLight(color.substring(4, 6), amount)}`;
vben
authored
5 years ago
74
}
vben
authored
5 years ago
75
76
77
78
79
80
81
/**
* Lightens a 6 char HEX color according to the passed percentage
* @param {string} color The color to change
* @param {number} amount The amount to change the color by
* @returns {string} The processed color represented as HEX
*/
vben
authored
5 years ago
82
export function lighten(color: string, amount: number) {
vben
authored
5 years ago
83
84
85
86
color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color;
amount = Math.trunc((255 * amount) / 100);
return `#${addLight(color.substring(0, 2), amount)}${addLight(
color.substring(2, 4),
vben
authored
4 years ago
87
amount,
vben
authored
5 years ago
88
)}${addLight(color.substring(4, 6), amount)}`;
vben
authored
5 years ago
89
}
vben
authored
5 years ago
90
91
92
93
94
95
96
97
/* Suma el porcentaje indicado a un color (RR, GG o BB) hexadecimal para aclararlo */
/**
* Sums the passed percentage to the R, G or B of a HEX color
* @param {string} color The color to change
* @param {number} amount The amount to change the color by
* @returns {string} The processed part of the color
*/
vben
authored
5 years ago
98
function addLight(color: string, amount: number) {
vben
authored
5 years ago
99
100
101
const cc = parseInt(color, 16) + amount;
const c = cc > 255 ? 255 : cc;
return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`;
vben
authored
5 years ago
102
}
vben
authored
5 years ago
103
104
105
106
107
108
109
/**
* Calculates luminance of an rgb color
* @param {number} r red
* @param {number} g green
* @param {number} b blue
*/
vben
authored
5 years ago
110
function luminanace(r: number, g: number, b: number) {
vben
authored
5 years ago
111
112
113
114
115
const a = [r, g, b].map((v) => {
v /= 255;
return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
});
return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
vben
authored
5 years ago
116
}
vben
authored
5 years ago
117
118
119
120
121
122
/**
* Calculates contrast between two rgb colors
* @param {string} rgb1 rgb color 1
* @param {string} rgb2 rgb color 2
*/
vben
authored
5 years ago
123
124
125
126
127
128
function contrast(rgb1: string[], rgb2: number[]) {
return (
(luminanace(~~rgb1[0], ~~rgb1[1], ~~rgb1[2]) + 0.05) /
(luminanace(rgb2[0], rgb2[1], rgb2[2]) + 0.05)
);
}
vben
authored
5 years ago
129
130
131
132
133
/**
* Determines what the best text color is (black or white) based con the contrast with the background
* @param hexColor - Last selected color by the user
*/
vben
authored
5 years ago
134
export function calculateBestTextColor(hexColor: string) {
vben
authored
5 years ago
135
136
137
138
const rgbColor = hexToRGB(hexColor.substring(1));
const contrastWithBlack = contrast(rgbColor.split(','), [0, 0, 0]);
return contrastWithBlack >= 12 ? '#000000' : '#FFFFFF';
vben
authored
5 years ago
139
}
vben
authored
5 years ago
140
141
142
143
144
145
146
/**
* Subtracts the indicated percentage to the R, G or B of a HEX color
* @param {string} color The color to change
* @param {number} amount The amount to change the color by
* @returns {string} The processed part of the color
*/
vben
authored
5 years ago
147
function subtractLight(color: string, amount: number) {
vben
authored
5 years ago
148
149
150
const cc = parseInt(color, 16) - amount;
const c = cc < 0 ? 0 : cc;
return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`;
vben
authored
5 years ago
151
}