File size: 1,106 Bytes
9cd6ddb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export const sleep = (ms: number) =>
  new Promise((resolve) => setTimeout(resolve, ms));

export const rgbaToHex = (rgba: string) => {
  if (!rgba.includes("rgba")) return "#121212";
  const [r, g, b, a] = rgba
    ?.replace("rgba(", "")
    ?.replace(")", "")
    ?.split(",")
    .map((v) => Number(v));
  return `#${((1 << 24) + (r << 16) + (g << 8) + b)?.toString(16)?.slice(1)}`;
};

export const hexToRgba = (hex: string, alpha: number) => {
  const r = parseInt(hex.slice(1, 3), 16);
  const g = parseInt(hex.slice(3, 5), 16);
  const b = parseInt(hex.slice(5, 7), 16);

  return `rgba(${r}, ${g}, ${b}, ${alpha})`;
};

export const hexToDecimal = (hex: string) => {
  return parseInt(hex, 16);
};

export const linearGradientToArrayOfHex = (gradient: string) => {
  const colours = gradient
    .replace("linear-gradient(", "")
    .replace(")", "")
    .split(",")
    .map((v) => v.trim());

  return colours.map((c) => rgbaToHex(c));
};

export const arrayOfHexsToLinearGradient = (
  colours: string[],
  angle: number
) => {
  return `linear-gradient(${angle}deg, ${colours.join(", ")})`;
};