Skip to content

Commit b117ef4

Browse files
committed
fix: named color type
1 parent 3c45262 commit b117ef4

2 files changed

Lines changed: 18 additions & 18 deletions

File tree

scripts/generate-colors.mts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import nc from 'nearest-color';
44
import path from 'path';
55
// import { minify } from 'terser';
66

7-
export type NamedColorType = {
7+
export type ColorType = {
88
name: string;
99
hex: string;
1010
};
1111

12-
export type Palette = NamedColorType[];
12+
export type Palette = ColorType[];
1313

1414
// Configuration constants
1515
export const CONFIG = {
@@ -28,12 +28,12 @@ const nearest = nc.from(
2828
);
2929

3030
// Cache for color lookups
31-
const colorCache = new Map<string, NamedColorType>();
31+
const colorCache = new Map<string, ColorType>();
3232

3333
// Optimized: Use Set for faster hex lookups
3434
const colorHexSet = new Set(colornames.map((c) => c.hex.toLowerCase()));
3535

36-
export function getColorName(hex: string): NamedColorType {
36+
export function getColorName(hex: string): ColorType {
3737
if (!hex || typeof hex !== 'string') {
3838
throw new Error('Invalid hex color provided');
3939
}
@@ -56,7 +56,7 @@ export function getColorName(hex: string): NamedColorType {
5656

5757
const resolved = exact?.name ?? nearest(fullHex)?.name ?? 'Unknown';
5858

59-
const result: NamedColorType = {
59+
const result: ColorType = {
6060
name: resolved,
6161
hex: fullHex
6262
};
@@ -74,13 +74,13 @@ export function generateRandomHexColor(): string {
7474
);
7575
}
7676

77-
export function generateColorList(count: number = CONFIG.TOTAL_COLORS): NamedColorType[] {
77+
export function generateColorList(count: number = CONFIG.TOTAL_COLORS): ColorType[] {
7878
if (count <= 0) {
7979
throw new Error('Count must be positive');
8080
}
8181

8282
const seen = new Set<string>();
83-
const list: NamedColorType[] = [];
83+
const list: ColorType[] = [];
8484
let attempts = 0;
8585
const maxAttempts = count * 10; // Prevent infinite loops
8686

@@ -134,7 +134,7 @@ function toTsObject(data: unknown, indent: number = 2): string {
134134
}
135135

136136
export function generatePalettesFromColorList(
137-
colorList: NamedColorType[],
137+
colorList: ColorType[],
138138
total: number,
139139
minLen: number = CONFIG.MIN_COLORS_PER_PALETTE,
140140
maxLen: number = CONFIG.MAX_COLORS_PER_PALETTE
@@ -153,7 +153,7 @@ export function generatePalettesFromColorList(
153153

154154
for (let i = 0; i < total; i++) {
155155
const length = Math.floor(Math.random() * (maxLen - minLen + 1)) + minLen;
156-
const palette = new Set<NamedColorType>();
156+
const palette = new Set<ColorType>();
157157
let attempts = 0;
158158
const maxAttempts = length * 10;
159159

@@ -170,9 +170,9 @@ export function generatePalettesFromColorList(
170170
}
171171

172172
// Improved error handling and parsing
173-
function parseColorsFromFile(content: string): NamedColorType[] {
173+
function parseColorsFromFile(content: string): ColorType[] {
174174
try {
175-
const colorsMatch = content.match(/export const colors: NamedColorType\[\] = (\[[\s\S]*?\]);/);
175+
const colorsMatch = content.match(/export const colors: ColorType\[\] = (\[[\s\S]*?\]);/);
176176
if (!colorsMatch) return [];
177177

178178
// Use safer parsing method
@@ -195,7 +195,7 @@ export async function generateColorsFile({
195195
createMinifiedCopy?: boolean;
196196
} = {}): Promise<void> {
197197
const outputPath = path.join(process.cwd(), relativeOutputPath);
198-
let colors: NamedColorType[] = [];
198+
let colors: ColorType[] = [];
199199

200200
// Try to reuse existing colors
201201
if (reuseExisting && existsSync(outputPath)) {
@@ -221,14 +221,14 @@ export async function generateColorsFile({
221221
console.log(`🎨 Generating ${CONFIG.TOTAL_PALETTES} new palettes...`);
222222
const palettes = generatePalettesFromColorList(colors, CONFIG.TOTAL_PALETTES, CONFIG.MIN_COLORS_PER_PALETTE, CONFIG.MAX_COLORS_PER_PALETTE);
223223

224-
const tsContent = `export type NamedColorType = {
224+
const tsContent = `export type ColorType = {
225225
name: string;
226226
hex: string;
227227
};
228228
229-
export type Palette = NamedColorType[];
229+
export type Palette = ColorType[];
230230
231-
export const colors: NamedColorType[] = ${toTsObject(colors)};
231+
export const colors: ColorType[] = ${toTsObject(colors)};
232232
233233
export const palettes: Palette[] = ${toTsObject(palettes)};
234234
`;

src/data/colors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
export type NamedColorType = {
1+
export type ColorType = {
22
name: string;
33
hex: string;
44
};
55

6-
export type Palette = NamedColorType[];
6+
export type Palette = ColorType[];
77

8-
export const colors: NamedColorType[] = [
8+
export const colors: ColorType[] = [
99
{ name: 'Orion', hex: '#db4eb6' },
1010
{ name: 'Forest Frolic', hex: '#89bd96' },
1111
{ name: 'Korean Mint', hex: '#597a5f' },

0 commit comments

Comments
 (0)