-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcolor.ts
More file actions
174 lines (159 loc) · 4.29 KB
/
color.ts
File metadata and controls
174 lines (159 loc) · 4.29 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import type { ColorStop } from "../core/types";
/**
* Converts an HSL color to a hex string.
*
* @param h - Hue (0-360).
* @param s - Saturation (0-100).
* @param l - Lightness (0-100).
* @returns Hex color string with #.
*/
function hslToHex(h: number, s: number, l: number): string {
const sNorm = s / 100;
const lNorm = l / 100;
const c = (1 - Math.abs(2 * lNorm - 1)) * sNorm;
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
const m = lNorm - c / 2;
let r = 0,
g = 0,
b = 0;
if (h < 60) {
r = c;
g = x;
} else if (h < 120) {
r = x;
g = c;
} else if (h < 180) {
g = c;
b = x;
} else if (h < 240) {
g = x;
b = c;
} else if (h < 300) {
r = x;
b = c;
} else {
r = c;
b = x;
}
return rgbToHex(
Math.round((r + m) * 255),
Math.round((g + m) * 255),
Math.round((b + m) * 255),
);
}
/**
* Generates an array of visually distinct colors using the golden angle
* hue distribution for maximum separation.
*
* @param count - Number of distinct colors to generate.
* @returns Array of hex color strings.
*/
export function generateDistinctColors(count: number): string[] {
const colors: string[] = [];
for (let i = 0; i < count; i++) {
const hue = (i * 137.508) % 360;
const saturation = 55 + (i % 3) * 15;
const lightness = 45 + (i % 2) * 10;
colors.push(hslToHex(hue, saturation, lightness));
}
return colors;
}
/**
* Converts a hex color to RGB values.
*
* @param hex - The hex color string (with or without #).
* @returns Object with r, g, b values (0-255).
*/
export function hexToRgb(
hex: string,
): { r: number; g: number; b: number } | null {
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result
? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
}
: null;
}
/**
* Converts RGB values to a hex color string.
*
* @param r - Red value (0-255).
* @param g - Green value (0-255).
* @param b - Blue value (0-255).
* @returns Hex color string with #.
*/
export function rgbToHex(r: number, g: number, b: number): string {
return "#" + [r, g, b].map((x) => x.toString(16).padStart(2, "0")).join("");
}
/**
* Interpolates between two colors.
*
* @param color1 - First hex color.
* @param color2 - Second hex color.
* @param factor - Interpolation factor (0-1).
* @returns Interpolated hex color.
*/
export function interpolateColor(
color1: string,
color2: string,
factor: number,
): string {
const rgb1 = hexToRgb(color1);
const rgb2 = hexToRgb(color2);
if (!rgb1 || !rgb2) {
return color1;
}
const r = Math.round(rgb1.r + (rgb2.r - rgb1.r) * factor);
const g = Math.round(rgb1.g + (rgb2.g - rgb1.g) * factor);
const b = Math.round(rgb1.b + (rgb2.b - rgb1.b) * factor);
return rgbToHex(r, g, b);
}
/**
* Gets a color from a colormap at a specific position.
*
* @param colorStops - Array of color stops.
* @param position - Position (0-1) to sample.
* @returns The interpolated color at the position.
*/
export function getColorAtPosition(
colorStops: ColorStop[],
position: number,
): string {
// Clamp position to valid range
const pos = Math.max(0, Math.min(1, position));
// Find surrounding color stops
let lower = colorStops[0];
let upper = colorStops[colorStops.length - 1];
for (let i = 0; i < colorStops.length - 1; i++) {
if (colorStops[i].position <= pos && colorStops[i + 1].position >= pos) {
lower = colorStops[i];
upper = colorStops[i + 1];
break;
}
}
// Handle edge cases
if (pos <= lower.position) return lower.color;
if (pos >= upper.position) return upper.color;
// Interpolate
const range = upper.position - lower.position;
const factor = range === 0 ? 0 : (pos - lower.position) / range;
return interpolateColor(lower.color, upper.color, factor);
}
/**
* Generates CSS gradient string from color stops.
*
* @param colorStops - Array of color stops.
* @param direction - CSS gradient direction.
* @returns CSS linear-gradient string.
*/
export function generateGradientCSS(
colorStops: ColorStop[],
direction: string = "to right",
): string {
const stops = colorStops
.map((stop) => `${stop.color} ${stop.position * 100}%`)
.join(", ");
return `linear-gradient(${direction}, ${stops})`;
}