-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor Scheme Extractor2.js
More file actions
401 lines (329 loc) · 12.7 KB
/
Color Scheme Extractor2.js
File metadata and controls
401 lines (329 loc) · 12.7 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* Color Scheme Extractor
* Analyzes and extracts the main color palette used on a website
* @param {Object} options - Configuration options
*/
(function extractColorScheme(options) {
'use strict';
options = options || {};
const config = {
includeTransparent: false, // Include transparent/rgba colors
minOccurrence: 3, // Minimum times a color must appear
groupSimilar: true, // Group similar colors together
similarityThreshold: 15, // Color difference threshold (0-255)
maxColors: 20, // Maximum colors to extract
includeGradients: true, // Extract gradient colors
scanDepth: 'full', // 'viewport' or 'full'
...options
};
// ============================================================================
// COLOR UTILITIES
// ============================================================================
function parseColor(colorStr) {
const canvas = document.createElement('canvas');
canvas.width = canvas.height = 1;
const ctx = canvas.getContext('2d');
ctx.fillStyle = colorStr;
ctx.fillRect(0, 0, 1, 1);
const imageData = ctx.getImageData(0, 0, 1, 1).data;
return {
r: imageData[0],
g: imageData[1],
b: imageData[2],
a: imageData[3] / 255,
original: colorStr
};
}
function rgbToHex(r, g, b) {
return '#' + [r, g, b].map(x => {
const hex = x.toString(16);
return hex.length === 1 ? '0' + hex : hex;
}).join('');
}
function colorDistance(c1, c2) {
// Euclidean distance in RGB space
return Math.sqrt(
Math.pow(c1.r - c2.r, 2) +
Math.pow(c1.g - c2.g, 2) +
Math.pow(c1.b - c2.b, 2)
);
}
function getLuminance(r, g, b) {
// Relative luminance (WCAG formula)
const [rs, gs, bs] = [r, g, b].map(c => {
c = c / 255;
return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
});
return 0.2126 * rs + 0.7152 * gs + 0.0722 * bs;
}
function getColorName(r, g, b) {
const luminance = getLuminance(r, g, b);
// Check for grayscale
if (Math.abs(r - g) < 10 && Math.abs(g - b) < 10 && Math.abs(r - b) < 10) {
if (luminance > 0.9) return 'White';
if (luminance < 0.1) return 'Black';
if (luminance > 0.7) return 'Light Gray';
if (luminance < 0.3) return 'Dark Gray';
return 'Gray';
}
// Dominant channel
const max = Math.max(r, g, b);
const saturation = (max - Math.min(r, g, b)) / (max || 1);
if (saturation < 0.2) {
return luminance > 0.5 ? 'Light Gray' : 'Dark Gray';
}
if (r === max && g > b) return luminance > 0.5 ? 'Light Orange' : 'Orange';
if (r === max && b > g) return luminance > 0.5 ? 'Light Pink' : 'Red';
if (r === max) return luminance > 0.5 ? 'Light Red' : 'Red';
if (g === max && r > b) return luminance > 0.5 ? 'Light Yellow' : 'Yellow';
if (g === max) return luminance > 0.5 ? 'Light Green' : 'Green';
if (b === max && r > g) return luminance > 0.5 ? 'Light Purple' : 'Purple';
if (b === max) return luminance > 0.5 ? 'Light Blue' : 'Blue';
return 'Unknown';
}
// ============================================================================
// COLOR EXTRACTION
// ============================================================================
const colorMap = new Map(); // hex -> {count, rgb, sources}
function addColor(colorStr, source) {
if (!colorStr || colorStr === 'none') return;
try {
const parsed = parseColor(colorStr);
// Skip transparent if configured
if (!config.includeTransparent && parsed.a < 0.1) return;
const hex = rgbToHex(parsed.r, parsed.g, parsed.b);
if (!colorMap.has(hex)) {
colorMap.set(hex, {
hex: hex,
rgb: parsed,
count: 0,
sources: new Set(),
luminance: getLuminance(parsed.r, parsed.g, parsed.b),
name: getColorName(parsed.r, parsed.g, parsed.b)
});
}
const entry = colorMap.get(hex);
entry.count++;
entry.sources.add(source);
} catch (e) {
// Invalid color, skip
}
}
function extractGradientColors(gradientStr) {
// Extract colors from gradient strings
const colorRegex = /(#[0-9a-f]{3,6}|rgba?\([^)]+\)|hsl\([^)]+\))/gi;
const matches = gradientStr.match(colorRegex);
if (matches) {
matches.forEach(color => addColor(color, 'gradient'));
}
}
// Get all elements to scan
const elements = config.scanDepth === 'viewport'
? Array.from(document.querySelectorAll('*')).filter(el => {
const rect = el.getBoundingClientRect();
return rect.top < window.innerHeight && rect.bottom > 0;
})
: document.querySelectorAll('*');
console.log('🔍 Scanning ' + elements.length + ' elements...');
// Extract colors from computed styles
elements.forEach(el => {
const styles = getComputedStyle(el);
// Text colors
addColor(styles.color, 'color');
// Background colors
addColor(styles.backgroundColor, 'background-color');
// Border colors
addColor(styles.borderTopColor, 'border-color');
addColor(styles.borderRightColor, 'border-color');
addColor(styles.borderBottomColor, 'border-color');
addColor(styles.borderLeftColor, 'border-color');
// Outline
addColor(styles.outlineColor, 'outline-color');
// Shadows
if (styles.boxShadow && styles.boxShadow !== 'none') {
extractGradientColors(styles.boxShadow);
}
if (styles.textShadow && styles.textShadow !== 'none') {
extractGradientColors(styles.textShadow);
}
// Gradients
if (config.includeGradients) {
if (styles.backgroundImage && styles.backgroundImage.includes('gradient')) {
extractGradientColors(styles.backgroundImage);
}
}
});
// ============================================================================
// COLOR GROUPING
// ============================================================================
let colors = Array.from(colorMap.values())
.filter(c => c.count >= config.minOccurrence)
.sort((a, b) => b.count - a.count);
if (config.groupSimilar) {
const grouped = [];
const used = new Set();
colors.forEach(color => {
if (used.has(color.hex)) return;
const group = {
primary: color,
variations: [],
totalCount: color.count
};
colors.forEach(other => {
if (color.hex === other.hex || used.has(other.hex)) return;
const distance = colorDistance(color.rgb, other.rgb);
if (distance <= config.similarityThreshold) {
group.variations.push(other);
group.totalCount += other.count;
used.add(other.hex);
}
});
used.add(color.hex);
grouped.push(group);
});
colors = grouped
.sort((a, b) => b.totalCount - a.totalCount)
.slice(0, config.maxColors)
.map(g => g.primary);
} else {
colors = colors.slice(0, config.maxColors);
}
// ============================================================================
// CATEGORIZE COLORS
// ============================================================================
const categories = {
primary: [],
text: [],
background: [],
accent: [],
neutral: []
};
colors.forEach(color => {
const sources = Array.from(color.sources);
// Categorization logic
if (color.name.includes('Gray') || color.name.includes('White') || color.name.includes('Black')) {
categories.neutral.push(color);
} else if (sources.includes('color')) {
categories.text.push(color);
} else if (sources.includes('background-color')) {
categories.background.push(color);
} else if (color.count > 10) {
categories.primary.push(color);
} else {
categories.accent.push(color);
}
});
// ============================================================================
// RESULTS
// ============================================================================
const results = {
totalColors: colorMap.size,
uniqueColors: colors.length,
categories: categories,
allColors: colors,
stats: {
mostUsed: colors[0],
leastUsed: colors[colors.length - 1],
totalOccurrences: colors.reduce((sum, c) => sum + c.count, 0)
}
};
// ============================================================================
// MARKDOWN GENERATION
// ============================================================================
function generateMarkdown() {
let md = '# Color Scheme Analysis\n\n';
md += '**Website:** `' + window.location.hostname + '`\n\n';
md += '**Stats:**\n';
md += '- Total unique colors found: ' + results.totalColors + '\n';
md += '- Colors after filtering: ' + results.uniqueColors + '\n';
md += '- Total color occurrences: ' + results.stats.totalOccurrences + '\n\n';
md += '---\n\n';
// Main colors table
md += '## Main Color Palette\n\n';
md += '| Preview | Hex | RGB | Name | Usage | Sources |\n';
md += '|---------|-----|-----|------|-------|----------|\n';
colors.forEach(color => {
const preview = ' + '/' + color.hex.slice(1) + '.png)';
const rgb = 'rgb(' + color.rgb.r + ', ' + color.rgb.g + ', ' + color.rgb.b + ')';
const sources = Array.from(color.sources).join(', ');
md += '| ' + preview + ' | `' + color.hex + '` | `' + rgb + '` | ' + color.name + ' | ' + color.count + 'x | ' + sources + ' |\n';
});
md += '\n';
// Categories
Object.entries(categories).forEach(([category, categoryColors]) => {
if (categoryColors.length === 0) return;
md += '## ' + category.charAt(0).toUpperCase() + category.slice(1) + ' Colors\n\n';
categoryColors.forEach(color => {
md += '- **' + color.name + '** `' + color.hex + '` - Used ' + color.count + ' times\n';
});
md += '\n';
});
// CSS Variables suggestion
md += '## CSS Variables Suggestion\n\n';
md += '```css\n:root {\n';
colors.slice(0, 10).forEach((color, i) => {
const varName = '--color-' + (color.name.toLowerCase().replace(/\s+/g, '-'));
md += ' ' + varName + ': ' + color.hex + ';\n';
});
md += '}\n```\n\n';
return md;
}
// ============================================================================
// SMART CLIPBOARD COPY
// ============================================================================
function smartCopy(text) {
if (document.hasFocus()) {
return navigator.clipboard.writeText(text)
.then(() => {
console.log('📋 Color scheme markdown copied to clipboard!');
return true;
})
.catch((err) => {
console.error('❌ Copy failed:', err.message);
console.log('💡 Tip: Click the page first, then run: result.copy()');
return false;
});
} else {
console.warn('⚠️ Document not focused. Click the page first, then run: result.copy()');
return Promise.resolve(false);
}
}
// ============================================================================
// CONSOLE OUTPUT
// ============================================================================
console.group('🎨 Color Scheme Analysis');
console.log('Total colors found:', results.totalColors);
console.log('Filtered colors:', results.uniqueColors);
console.group('📊 Main Colors');
colors.forEach(color => {
console.log(
'%c %c ' + color.hex + ' - ' + color.name + ' (' + color.count + 'x)',
'background:' + color.hex + ';padding:10px;border:1px solid #ccc',
'color:inherit'
);
});
console.groupEnd();
Object.entries(categories).forEach(([category, categoryColors]) => {
if (categoryColors.length === 0) return;
console.group('📁 ' + category.charAt(0).toUpperCase() + category.slice(1));
categoryColors.forEach(color => {
console.log(
'%c %c ' + color.hex + ' (' + color.count + 'x)',
'background:' + color.hex + ';padding:5px;border:1px solid #ccc',
'color:inherit'
);
});
console.groupEnd();
});
console.groupEnd();
// ============================================================================
// MARKDOWN OUTPUT & COPY
// ============================================================================
const markdown = generateMarkdown();
results.markdown = markdown;
results.copy = function() {
return smartCopy(this.markdown);
};
smartCopy(markdown);
return results;
})();