-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfadeColor.js
102 lines (84 loc) · 2.69 KB
/
fadeColor.js
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
const HexColor = new RegExp(/#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/)
const RGBColor = new RegExp(/rgb\((\d{1,3}), (\d{1,3}), (\d{1,3})\)/)
const COLOR_TYPE = {
RGB: 'rgb',
HEX: 'hex'
}
function isHexColor(color) {
return HexColor.test(color)
}
function isRGBColor(color) {
return RGBColor.test(color)
}
function interpolateColor(color1, color2, factor) {
if (arguments.length < 3) {
factor = 0.5;
}
let result = color1.slice();
for (let i = 0; i < 3; i++) {
result[i] = Math.round(result[i] + factor * (color2[i] - color1[i]));
}
return result;
};
async function hexConvertToRGB(color) {
const hexColor = color.replace("#","")
var aRgbHex = hexColor.match(/.{1,2}/g);
const colorCode = `${parseInt(aRgbHex[0], 16)}, ${parseInt(aRgbHex[1], 16)}, ${parseInt(aRgbHex[2], 16)}`
return `rgb(${colorCode})`;
}
function convertToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function RGBToHEXConvert([r, g, b]) {
return "#" + convertToHex(r) + convertToHex(g) + convertToHex(b);
}
async function isValidColor(color1, color2) {
if (isHexColor(color1) && isHexColor(color2)) {
return { isValid: true, colorType: COLOR_TYPE.HEX };
}
else if (isRGBColor(color1) && isRGBColor(color2)) {
return { isValid: true, colorType: COLOR_TYPE.RGB };
}
else {
return { isValid: false, colorType: null };
}
}
async function fadeColor(color1, color2, steps, location= 0, returnType = COLOR_TYPE.HEX) {
let selectedColorOne = color1;
let selectedColorTwo = color2;
const { isValid, colorType } = await isValidColor(color1, color2);
if (!isValid) {
throw new Error('Invalid color!')
}
if (colorType === COLOR_TYPE.HEX) {
selectedColorOne = await hexConvertToRGB(color1);
selectedColorTwo = await hexConvertToRGB(color2);
}
let stepFactor = 1 / (steps - 1),
interpolatedColorArray = [];
color1 = selectedColorOne.match(/\d+/g).map(Number);
color2 = selectedColorTwo.match(/\d+/g).map(Number);
for (let i = 0; i < steps; i++) {
interpolatedColorArray.push(interpolateColor(color1, color2, stepFactor * i));
}
if(location === 0){
if(returnType === COLOR_TYPE.HEX){
return interpolatedColorArray.map((each)=>{
return RGBToHEXConvert(each);
});
}
else{
return interpolatedColorArray
}
}else{
if(returnType === COLOR_TYPE.HEX){
return RGBToHEXConvert(interpolatedColorArray[location-1]);
}else{
return interpolatedColorArray[location-1];
}
}
}
module.exports = {
fadeColor
}