|
| 1 | +// Function to convert hex to RGB |
1 | 2 | function hexToRgb(hex) { |
2 | | - let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); |
3 | | - return result |
4 | | - ? { |
5 | | - r: parseInt(result[1], 16), |
6 | | - g: parseInt(result[2], 16), |
7 | | - b: parseInt(result[3], 16), |
8 | | - } |
9 | | - : null; |
| 3 | + hex = hex.replace("#", ""); |
| 4 | + const bigint = parseInt(hex, 16); |
| 5 | + const r = (bigint >> 16) & 255; |
| 6 | + const g = (bigint >> 8) & 255; |
| 7 | + const b = bigint & 255; |
| 8 | + return [r, g, b]; |
10 | 9 | } |
11 | 10 |
|
| 11 | +// Function to convert RGB to hex |
12 | 12 | function rgbToHex(r, g, b) { |
13 | | - return ( |
14 | | - "#" + |
15 | | - ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase() |
16 | | - ); |
| 13 | + return `#${((1 << 24) + (r << 16) + (g << 8) + b) |
| 14 | + .toString(16) |
| 15 | + .slice(1) |
| 16 | + .toUpperCase()}`; |
17 | 17 | } |
18 | 18 |
|
19 | | -function interpolateColor(startRgb, endRgb, factor) { |
20 | | - return { |
21 | | - r: Math.round(startRgb.r + factor * (endRgb.r - startRgb.r)), |
22 | | - g: Math.round(startRgb.g + factor * (endRgb.g - startRgb.g)), |
23 | | - b: Math.round(startRgb.b + factor * (endRgb.b - startRgb.b)), |
24 | | - }; |
| 19 | +// Function to interpolate between two colors |
| 20 | +function interpolateColor(color1, color2, factor) { |
| 21 | + const result = color1.slice(); |
| 22 | + for (let i = 0; i < 3; i++) { |
| 23 | + result[i] = Math.round(result[i] + factor * (color2[i] - result[i])); |
| 24 | + } |
| 25 | + return result; |
25 | 26 | } |
26 | 27 |
|
27 | 28 | // Function to generate the gradient |
|
0 commit comments