(color: RGB, alpha: number = 1)
| 78 | * Convert RGB color to CSS color string |
| 79 | */ |
| 80 | export const htmlColor = (color: RGB, alpha: number = 1): string => { |
| 81 | if (color.r === 1 && color.g === 1 && color.b === 1 && alpha === 1) { |
| 82 | return "white"; |
| 83 | } |
| 84 | if (color.r === 0 && color.g === 0 && color.b === 0 && alpha === 1) { |
| 85 | return "black"; |
| 86 | } |
| 87 | if (alpha === 1) { |
| 88 | const r = Math.round(color.r * 255); |
| 89 | const g = Math.round(color.g * 255); |
| 90 | const b = Math.round(color.b * 255); |
| 91 | const toHex = (num: number): string => num.toString(16).padStart(2, "0"); |
| 92 | return `#${toHex(r)}${toHex(g)}${toHex(b)}`.toUpperCase(); |
| 93 | } |
| 94 | const r = numberToFixedString(color.r * 255); |
| 95 | const g = numberToFixedString(color.g * 255); |
| 96 | const b = numberToFixedString(color.b * 255); |
| 97 | const a = numberToFixedString(alpha); |
| 98 | return `rgba(${r}, ${g}, ${b}, ${a})`; |
| 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * Process a single gradient stop |
no test coverage detected