color-util-helpers 1.0.4 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +47 -0
  2. package/ng-package.json +8 -0
  3. package/package.json +2 -15
  4. package/src/lib/assets/picture.webp +0 -0
  5. package/src/lib/color-conversion.service.spec.ts +54 -0
  6. package/src/lib/color-conversion.service.ts +35 -0
  7. package/src/lib/color-extractor.directive.spec.ts +49 -0
  8. package/src/lib/color-extractor.directive.ts +28 -0
  9. package/src/lib/color-grab.directive.ts +204 -0
  10. package/src/lib/color-lighten-darken.service.spec.ts +61 -0
  11. package/src/lib/color-lighten-darken.service.ts +83 -0
  12. package/src/lib/color-pallette.service.spec.ts +85 -0
  13. package/src/lib/color-pallette.service.ts +191 -0
  14. package/src/lib/color-scheme.service.ts +123 -0
  15. package/src/lib/color-utilities-demo/color-utilities-demo.component.css +12 -0
  16. package/src/lib/color-utilities-demo/color-utilities-demo.component.html +109 -0
  17. package/src/lib/color-utilities-demo/color-utilities-demo.component.ts +57 -0
  18. package/src/lib/color-utils.module.ts +27 -0
  19. package/src/lib/text-color.service.spec.ts +75 -0
  20. package/src/lib/text-color.service.ts +101 -0
  21. package/{public-api.d.ts → src/public-api.ts} +7 -0
  22. package/tsconfig.lib.json +32 -0
  23. package/tsconfig.lib.prod.json +10 -0
  24. package/tsconfig.spec.json +14 -0
  25. package/color-util-helpers-1.0.4.tgz +0 -0
  26. package/esm2022/color-util-helpers.mjs +0 -5
  27. package/esm2022/lib/color-conversion.service.mjs +0 -39
  28. package/esm2022/lib/color-extractor.directive.mjs +0 -37
  29. package/esm2022/lib/color-lighten-darken.service.mjs +0 -79
  30. package/esm2022/lib/color-pallette.service.mjs +0 -172
  31. package/esm2022/lib/color-scheme.service.mjs +0 -113
  32. package/esm2022/lib/color-utilities-demo/color-utilities-demo.component.mjs +0 -41
  33. package/esm2022/lib/color-utils.module.mjs +0 -32
  34. package/esm2022/lib/text-color.service.mjs +0 -79
  35. package/esm2022/public-api.mjs +0 -12
  36. package/fesm2022/color-util-helpers.mjs +0 -580
  37. package/fesm2022/color-util-helpers.mjs.map +0 -1
  38. package/index.d.ts +0 -5
  39. package/lib/color-conversion.service.d.ts +0 -8
  40. package/lib/color-extractor.directive.d.ts +0 -13
  41. package/lib/color-lighten-darken.service.d.ts +0 -10
  42. package/lib/color-pallette.service.d.ts +0 -36
  43. package/lib/color-scheme.service.d.ts +0 -45
  44. package/lib/color-utilities-demo/color-utilities-demo.component.d.ts +0 -34
  45. package/lib/color-utils.module.d.ts +0 -10
  46. package/lib/text-color.service.d.ts +0 -18
@@ -1,580 +0,0 @@
1
- import * as i0 from '@angular/core';
2
- import { NgModule, Injectable, inject, Component, EventEmitter, Directive, Output, HostListener } from '@angular/core';
3
- import * as i1 from '@angular/common';
4
- import { CommonModule } from '@angular/common';
5
- import { MatButtonModule } from '@angular/material/button';
6
- import * as i2 from '@angular/material/divider';
7
- import { MatDividerModule } from '@angular/material/divider';
8
- import { BehaviorSubject } from 'rxjs';
9
-
10
- class ColorUtilitiesModule {
11
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorUtilitiesModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
12
- static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.2.12", ngImport: i0, type: ColorUtilitiesModule, declarations: [ColorUtilitiesDemoComponent], imports: [CommonModule,
13
- MatButtonModule,
14
- MatDividerModule], exports: [ColorUtilitiesDemoComponent] }); }
15
- static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorUtilitiesModule, imports: [CommonModule,
16
- MatButtonModule,
17
- MatDividerModule] }); }
18
- }
19
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorUtilitiesModule, decorators: [{
20
- type: NgModule,
21
- args: [{
22
- imports: [
23
- CommonModule,
24
- MatButtonModule,
25
- MatDividerModule
26
- ],
27
- declarations: [
28
- ColorUtilitiesDemoComponent,
29
- ],
30
- exports: [
31
- ColorUtilitiesDemoComponent
32
- ]
33
- }]
34
- }] });
35
-
36
- class ColorConversionService {
37
- constructor() {
38
- this.componentToHex = (c) => {
39
- const hex = c.toString(16);
40
- return hex.length === 1 ? "0" + hex : hex;
41
- };
42
- }
43
- rgbToHex(rgb) {
44
- if (rgb === null || rgb.length !== 3 || rgb.some(value => value < 0 || value > 255))
45
- return '';
46
- const [r, g, b] = rgb;
47
- const hexR = this.componentToHex(r);
48
- const hexG = this.componentToHex(g);
49
- const hexB = this.componentToHex(b);
50
- return "#" + hexR + hexG + hexB;
51
- }
52
- hexToRgb(hex) {
53
- if (hex === null || hex === undefined)
54
- return [];
55
- hex = (hex.length === 3) ? hex + hex : hex;
56
- const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
57
- return result ? [
58
- parseInt(result[1], 16),
59
- parseInt(result[2], 16),
60
- parseInt(result[3], 16)
61
- ] : [];
62
- }
63
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorConversionService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
64
- static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorConversionService, providedIn: 'root' }); }
65
- }
66
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorConversionService, decorators: [{
67
- type: Injectable,
68
- args: [{
69
- providedIn: 'root'
70
- }]
71
- }] });
72
-
73
- class ColorPalletteService {
74
- constructor(colorConversionService) {
75
- this.colorConversionService = colorConversionService;
76
- // define image path
77
- // this.colorSelectionService.getColorsFromImage('../assets/sample2.jpg')
78
- // get colors
79
- // this.colorSelectionService.palette.subscribe(data => this.palette = data)
80
- // sample html
81
- // <div *ngFor="let color of palette">
82
- // <div style="display: flex;">
83
- // <div
84
- // class="box"
85
- // [style.background-color]="color.color"
86
- // >
87
- // Color
88
- // </div>
89
- // <div
90
- // class="box"
91
- // [style.background-color]="color.complementaryColor"
92
- // >
93
- // Complementary
94
- // </div>
95
- // </div>
96
- // </div>
97
- // CSS
98
- // .box {
99
- // width: 100px;
100
- // height: 100px;
101
- // border: solid thin black;
102
- // color: black;
103
- // margin: 4px;
104
- // padding: 16px;
105
- // display: flex;
106
- // flex-wrap: wrap;
107
- // align-content: center;
108
- // justify-content: center;
109
- // }
110
- this.palette = new BehaviorSubject([]);
111
- this.palette$ = this.palette.asObservable();
112
- }
113
- /**
114
- * Retrieves a color palette from an image at the specified path.
115
- *
116
- * @param imagePath - The path to the image to extract the color palette from.
117
- * @param colors - The number of colors to include in the palette (default is 3).
118
- * @returns An observable that emits the generated color palette.
119
- */
120
- getColorsFromImage(imagePath, colors = 3) {
121
- const image = new Image();
122
- image.src = imagePath;
123
- image.onload = () => {
124
- const data = this.generateColorPalette(image, colors) || [];
125
- this.palette.next(data);
126
- };
127
- }
128
- /**
129
- * Generates a color palette from an image.
130
- *
131
- * @param image - The HTML image element to extract the color palette from.
132
- * @param colorCount - The number of colors to include in the palette (default is 6).
133
- * @returns An array of color objects, each with a hex color and a complementary hex color.
134
- */
135
- generateColorPalette(image, colorCount = 6) {
136
- const canvas = document.createElement("canvas");
137
- const context = canvas.getContext("2d");
138
- if (!context)
139
- return;
140
- canvas.width = image.width;
141
- canvas.height = image.height;
142
- context.drawImage(image, 0, 0);
143
- // Get the image data
144
- const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
145
- const pixels = imageData.data;
146
- const pixelCount = imageData.width * imageData.height;
147
- // Build an array of RGB colors
148
- const colors = [];
149
- for (let i = 0; i < pixelCount; i++) {
150
- const offset = i * 4;
151
- const r = pixels[offset];
152
- const g = pixels[offset + 1];
153
- const b = pixels[offset + 2];
154
- colors.push([r, g, b]);
155
- }
156
- // Apply color quantization using k-means clustering
157
- const quantizedColors = this.kMeansColorQuantization(colors, colorCount);
158
- // Order colors by luminance
159
- quantizedColors.sort((color1, color2) => {
160
- const luminance1 = this.getLuminance(color1);
161
- const luminance2 = this.getLuminance(color2);
162
- return luminance2 - luminance1;
163
- });
164
- const palette = quantizedColors.map((color) => {
165
- const complementaryColor = color.map((component) => 255 - component);
166
- const hexColor = this.colorConversionService.rgbToHex(color);
167
- const hexComplementaryColor = this.colorConversionService.rgbToHex(complementaryColor);
168
- return { color: hexColor, complementaryColor: hexComplementaryColor };
169
- });
170
- return palette;
171
- }
172
- getLuminance(color) {
173
- const [r, g, b] = color;
174
- return 0.299 * r + 0.587 * g + 0.114 * b;
175
- }
176
- calculateColorDistance(color1, color2) {
177
- const [r1, g1, b1] = color1;
178
- const [r2, g2, b2] = color2;
179
- const dr = r2 - r1;
180
- const dg = g2 - g1;
181
- const db = b2 - b1;
182
- return Math.sqrt(dr * dr + dg * dg + db * db);
183
- }
184
- calculateMeanColor(colors) {
185
- let sumR = 0;
186
- let sumG = 0;
187
- let sumB = 0;
188
- for (let i = 0; i < colors.length; i++) {
189
- const [r, g, b] = colors[i];
190
- sumR += r;
191
- sumG += g;
192
- sumB += b;
193
- }
194
- const meanR = Math.round(sumR / colors.length);
195
- const meanG = Math.round(sumG / colors.length);
196
- const meanB = Math.round(sumB / colors.length);
197
- return [meanR, meanG, meanB];
198
- }
199
- kMeansColorQuantization(colors, k) {
200
- let clusterCenters = [];
201
- for (let i = 0; i < k; i++) {
202
- const randomColor = colors[Math.floor(Math.random() * colors.length)];
203
- clusterCenters.push(randomColor);
204
- }
205
- let clusters = [];
206
- for (let i = 0; i < colors.length; i++) {
207
- const color = colors[i];
208
- let minDistance = Infinity;
209
- let nearestCenter = null;
210
- for (let j = 0; j < clusterCenters.length; j++) {
211
- const center = clusterCenters[j];
212
- const distance = this.calculateColorDistance(color, center);
213
- if (distance < minDistance) {
214
- minDistance = distance;
215
- nearestCenter = center;
216
- }
217
- }
218
- clusters.push({ color, center: nearestCenter });
219
- }
220
- let updatedCenters = [];
221
- for (let i = 0; i < clusterCenters.length; i++) {
222
- const center = clusterCenters[i];
223
- const clusterColors = clusters.filter(c => c.center === center).map(c => c.color);
224
- if (clusterColors.length > 0) {
225
- const updatedCenter = this.calculateMeanColor(clusterColors);
226
- updatedCenters.push(updatedCenter);
227
- }
228
- }
229
- return updatedCenters;
230
- }
231
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorPalletteService, deps: [{ token: ColorConversionService }], target: i0.ɵɵFactoryTarget.Injectable }); }
232
- static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorPalletteService, providedIn: 'root' }); }
233
- }
234
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorPalletteService, decorators: [{
235
- type: Injectable,
236
- args: [{
237
- providedIn: 'root'
238
- }]
239
- }], ctorParameters: function () { return [{ type: ColorConversionService }]; } });
240
-
241
- class TextColorService {
242
- constructor(colorConversionService) {
243
- this.colorConversionService = colorConversionService;
244
- }
245
- textColorForBgColor(bgColor, lightColor, darkColor) {
246
- const UIColors = this.fixColor(bgColor);
247
- const r = UIColors[0];
248
- const g = UIColors[1];
249
- const b = UIColors[2];
250
- return ((r * 0.299 + g * 0.587 + b * 0.114) > 149) ? darkColor : lightColor;
251
- }
252
- darkerColor(color1, color2) {
253
- return this.isColorDarker(color1, color2) ? color1 : color2;
254
- }
255
- isColorDarker(color1, color2) {
256
- const newColor1 = this.fixColor(color1);
257
- const newColor2 = this.fixColor(color2);
258
- const luminance1 = this.calculateLuminance(newColor1[0], newColor1[1], newColor1[2]);
259
- const luminance2 = this.calculateLuminance(newColor2[0], newColor2[1], newColor2[2]);
260
- return luminance1 < luminance2;
261
- }
262
- lighterColor(color1, color2) {
263
- return (this.isColorLighter(color1, color2)) ? color1 : color2;
264
- }
265
- isColorLighter(color1, color2) {
266
- const newColor1 = this.fixColor(color1);
267
- const newColor2 = this.fixColor(color2);
268
- const luminance1 = this.calculateLuminance(newColor1[0], newColor1[1], newColor1[2]);
269
- const luminance2 = this.calculateLuminance(newColor2[0], newColor2[1], newColor2[2]);
270
- return (luminance1 > luminance2);
271
- }
272
- calculateLuminance(r, g, b) {
273
- return 0.2126 * r + 0.7152 * g + 0.0722 * b;
274
- }
275
- fixColor(color) {
276
- // Remove leading hash if present
277
- const sanitizedColor = color.startsWith('#') ? color.slice(1) : color;
278
- // Validate if the color is a valid hex (3 or 6 characters)
279
- if (!this.isValidHex(sanitizedColor)) {
280
- return this.parseRgb(sanitizedColor); // If not hex, attempt to parse as RGB
281
- }
282
- // Convert hex to RGB
283
- const rgb = this.hexToRgb(sanitizedColor);
284
- return rgb;
285
- }
286
- // Helper function to validate if a string is a valid 3 or 6 digit hex code
287
- isValidHex(color) {
288
- const hexRegex = /^[A-Fa-f0-9]{3}$|^[A-Fa-f0-9]{6}$/i;
289
- return hexRegex.test(color);
290
- }
291
- // Helper function to convert a 3 or 6 digit hex color to RGB
292
- hexToRgb(hex) {
293
- if (hex.length === 3) {
294
- hex = hex.split('').map(c => c + c).join(''); // Expand shorthand hex to full
295
- }
296
- return [
297
- parseInt(hex.slice(0, 2), 16),
298
- parseInt(hex.slice(2, 4), 16),
299
- parseInt(hex.slice(4, 6), 16),
300
- ];
301
- }
302
- // Helper function to parse an RGB string (e.g., rgb(255, 0, 0)) into an array
303
- parseRgb(rgb) {
304
- const match = rgb.match(/\d+/g);
305
- return match ? match.map(num => parseInt(num)) : [];
306
- }
307
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TextColorService, deps: [{ token: ColorConversionService }], target: i0.ɵɵFactoryTarget.Injectable }); }
308
- static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TextColorService, providedIn: 'root' }); }
309
- }
310
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TextColorService, decorators: [{
311
- type: Injectable,
312
- args: [{
313
- providedIn: 'root'
314
- }]
315
- }], ctorParameters: function () { return [{ type: ColorConversionService }]; } });
316
-
317
- class ColorLightenDarkenService {
318
- // const color = '#3498db'; // Your color
319
- // const lighterColor = lighten(color, 0.2); // 20% lighter
320
- // const darkerColor = darken(color, 0.2); // 20% darker
321
- // console.log(lighterColor, darkerColor);
322
- constructor() {
323
- this.colors = inject(TextColorService);
324
- }
325
- lighten(color, amount) {
326
- const rgb = this.colors.fixColor(color);
327
- // const rgb = color.match(/\w\w/g)?.map((x) => parseInt(x, 16)) || [];
328
- // Convert RGB to HSL
329
- let [r, g, b] = rgb.map((c) => c / 255);
330
- const max = Math.max(r, g, b), min = Math.min(r, g, b);
331
- let h = 0, s = 0, l = (max + min) / 2;
332
- if (max !== min) {
333
- const d = max - min;
334
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
335
- switch (max) {
336
- case r:
337
- h = (g - b) / d + (g < b ? 6 : 0);
338
- break;
339
- case g:
340
- h = (b - r) / d + 2;
341
- break;
342
- case b:
343
- h = (r - g) / d + 4;
344
- break;
345
- }
346
- h /= 6;
347
- }
348
- // Modify the lightness and clamp it to [0, 1]
349
- l = Math.min(1, l + amount);
350
- // Convert HSL back to RGB
351
- if (s === 0) {
352
- r = g = b = l; // achromatic
353
- }
354
- else {
355
- const hue2rgb = (p, q, t) => {
356
- if (t < 0)
357
- t += 1;
358
- if (t > 1)
359
- t -= 1;
360
- if (t < 1 / 6)
361
- return p + (q - p) * 6 * t;
362
- if (t < 1 / 2)
363
- return q;
364
- if (t < 2 / 3)
365
- return p + (q - p) * (2 / 3 - t) * 6;
366
- return p;
367
- };
368
- const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
369
- const p = 2 * l - q;
370
- r = hue2rgb(p, q, h + 1 / 3);
371
- g = hue2rgb(p, q, h);
372
- b = hue2rgb(p, q, h - 1 / 3);
373
- }
374
- // Convert RGB back to hexadecimal color
375
- const toHex = (x) => Math.round(x * 255)
376
- .toString(16)
377
- .padStart(2, '0');
378
- return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
379
- }
380
- darken(color, amount) {
381
- return this.lighten(color, -amount);
382
- }
383
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorLightenDarkenService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
384
- static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorLightenDarkenService, providedIn: 'root' }); }
385
- }
386
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorLightenDarkenService, decorators: [{
387
- type: Injectable,
388
- args: [{
389
- providedIn: 'root'
390
- }]
391
- }], ctorParameters: function () { return []; } });
392
-
393
- class ColorSchemeService {
394
- constructor() { }
395
- /**
396
- * Generates a random hexadecimal color code.
397
- *
398
- * This function generates a random hue value between 0 and 360 degrees, and random saturation and lightness values between 50% and 100%. It then converts the HSL values to RGB values using the `hslToRgb` function, and finally converts the RGB values to a hexadecimal color code using the `rgbToHex` function.
399
- *
400
- * @returns A hexadecimal color code in the format "#RRGGBB".
401
- */
402
- generateRandomColor() {
403
- // Generate a random hue between 0 and 360 (representing degrees on the color wheel)
404
- const hue = Math.floor(Math.random() * 360);
405
- // Generate random saturation and lightness values between 50% and 100%
406
- const saturation = Math.floor(Math.random() * 51) + 50;
407
- const lightness = Math.floor(Math.random() * 51) + 50;
408
- // Convert HSL values to RGB values
409
- const rgbColor = this.hslToRgb(hue, saturation, lightness);
410
- // Convert RGB values to hexadecimal color code
411
- const hexColor = this.rgbToHex(rgbColor.r, rgbColor.g, rgbColor.b);
412
- return hexColor;
413
- }
414
- /**
415
- * Converts HSL (Hue, Saturation, Lightness) color values to RGB (Red, Green, Blue) color values.
416
- *
417
- * @param h - The hue value, ranging from 0 to 360 degrees.
418
- * @param s - The saturation value, ranging from 0 to 100 percent.
419
- * @param l - The lightness value, ranging from 0 to 100 percent.
420
- * @returns An object with the RGB color values, where each value is between 0 and 255.
421
- */
422
- hslToRgb(h, s, l) {
423
- h /= 360;
424
- s /= 100;
425
- l /= 100;
426
- let r, g, b;
427
- if (s === 0) {
428
- r = g = b = l; // Achromatic color (gray)
429
- }
430
- else {
431
- const hueToRgb = (p, q, t) => {
432
- if (t < 0)
433
- t += 1;
434
- if (t > 1)
435
- t -= 1;
436
- if (t < 1 / 6)
437
- return p + (q - p) * 6 * t;
438
- if (t < 1 / 2)
439
- return q;
440
- if (t < 2 / 3)
441
- return p + (q - p) * (2 / 3 - t) * 6;
442
- return p;
443
- };
444
- const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
445
- const p = 2 * l - q;
446
- r = Math.round(hueToRgb(p, q, h + 1 / 3) * 255);
447
- g = Math.round(hueToRgb(p, q, h) * 255);
448
- b = Math.round(hueToRgb(p, q, h - 1 / 3) * 255);
449
- }
450
- return { r, g, b };
451
- }
452
- /**
453
- * Converts RGB color values to a hexadecimal color string.
454
- *
455
- * @param r - The red color value, between 0 and 255.
456
- * @param g - The green color value, between 0 and 255.
457
- * @param b - The blue color value, between 0 and 255.
458
- * @returns A hexadecimal color string in the format "#RRGGBB".
459
- */
460
- rgbToHex(r, g, b) {
461
- const componentToHex = (c) => {
462
- const hex = c.toString(16);
463
- return hex.length === 1 ? "0" + hex : hex;
464
- };
465
- return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
466
- }
467
- /**
468
- * Adjusts a hexadecimal color value by a given percentage.
469
- *
470
- * @param hexColor - The hexadecimal color value to adjust.
471
- * @param percentage - The percentage to adjust the color by, ranging from -100 to 100.
472
- * @returns The adjusted hexadecimal color value.
473
- */
474
- adjustHexColor(hexColor, percentage) {
475
- // Remove the "#" symbol if present
476
- hexColor = hexColor.replace("#", "");
477
- // Convert the hex color to RGB values
478
- const red = parseInt(hexColor.substring(0, 2), 16);
479
- const green = parseInt(hexColor.substring(2, 4), 16);
480
- const blue = parseInt(hexColor.substring(4, 6), 16);
481
- // Calculate the adjustment amount based on the percentage
482
- const adjustAmount = Math.round(255 * (percentage / 100));
483
- // Adjust the RGB values
484
- const adjustedRed = this.clamp(red + adjustAmount);
485
- const adjustedGreen = this.clamp(green + adjustAmount);
486
- const adjustedBlue = this.clamp(blue + adjustAmount);
487
- // Convert the adjusted RGB values back to hex
488
- const adjustedHexColor = `#${(adjustedRed).toString(16).padStart(2, '0')}${(adjustedGreen).toString(16).padStart(2, '0')}${(adjustedBlue).toString(16).padStart(2, '0')}`;
489
- return adjustedHexColor;
490
- }
491
- clamp(value) {
492
- return Math.max(0, Math.min(value, 255));
493
- }
494
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorSchemeService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); }
495
- static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorSchemeService, providedIn: 'root' }); }
496
- }
497
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorSchemeService, decorators: [{
498
- type: Injectable,
499
- args: [{
500
- providedIn: 'root'
501
- }]
502
- }], ctorParameters: function () { return []; } });
503
-
504
- class ColorUtilitiesDemoComponent {
505
- constructor() {
506
- this.colorConversionService = inject(ColorConversionService);
507
- this.colorLightenDarkenService = inject(ColorLightenDarkenService);
508
- this.colorPalletteService = inject(ColorPalletteService);
509
- this.textColorService = inject(TextColorService);
510
- this.colorSchemeService = inject(ColorSchemeService);
511
- this.HEX = this.colorConversionService.rgbToHex([12, 56, 128]);
512
- this.RGB = `rgb(${this.colorConversionService.hexToRgb('#AA11BB')})`;
513
- this.lighten = this.colorLightenDarkenService.lighten('#AA11BB', .25);
514
- this.darken = this.colorLightenDarkenService.darken('#AA11BB', .25);
515
- this.colorIsDarker = this.textColorService.isColorDarker(this.lighten, this.darken);
516
- this.darkBk = this.textColorService.textColorForBgColor(this.HEX, this.lighten, this.darken);
517
- this.lightBk = this.textColorService.textColorForBgColor('whitesmoke', this.lighten, this.darken);
518
- this.colors$ = this.colorPalletteService.palette$;
519
- this.colorPick = this.colorSchemeService.generateRandomColor();
520
- this.colorPickDarker = this.colorSchemeService.adjustHexColor(this.colorPick, -25);
521
- this.colorPickLighter = this.colorSchemeService.adjustHexColor(this.colorPick, 25);
522
- }
523
- ngOnInit() {
524
- // define image path
525
- this.img = '/assets/images/HD.png';
526
- this.colorPalletteService.getColorsFromImage(this.img, 8);
527
- }
528
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorUtilitiesDemoComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
529
- static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "16.2.12", type: ColorUtilitiesDemoComponent, selector: "app-color-utilities-demo", ngImport: i0, template: "<div style=\"margin: 2rem;\">\n\n <h1>Color Conversion Service</h1>\n <div style=\"display: flex; flex-direction: column; gap: 1rem;\">\n <div style=\"display: flex\">\n <div style=\"padding-top: .5rem; margin-right: .5rem;\">rgbToHex: {{ HEX }}</div>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"HEX\"></div>\n </div>\n\n <div style=\"display: flex\">\n <div style=\"padding-top: .5rem; margin-right: .5rem;\"> hexToRgb: {{ RGB }} </div>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"RGB\"></div>\n </div>\n </div>\n\n <div style=\"margin-top: 1rem; margin-bottom: 1rem;\">\n <mat-divider></mat-divider>\n </div>\n\n <h1>Color Light/Darken Service</h1>\n\n <div style=\"display: flex; flex-direction: column; gap: 1rem;\">\n\n <div style=\"display: flex; gap: 1rem\">\n Original Color: #AA11BB<br>\n <div style=\"width: 32px; height: 32px; background-color: #AA11BB;\"></div>\n Lighten (25%): {{ lighten }}<br>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"lighten\"></div>\n Darken (25%): {{ darken }}<br>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"darken\"></div>\n </div>\n\n </div>\n\n <div style=\"margin-top: 1rem; margin-bottom: 1rem;\">\n <mat-divider></mat-divider>\n </div>\n\n <h1>Text Color Utility Services</h1>\n\n <div style=\"display: flex; gap: 1rem; flex-direction: column;\">\n <div style=\"display: flex; gap: 1rem\">\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"darken\"></div>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"lighten\"></div>\n is Darker : {{ colorIsDarker }}\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"colorIsDarker\"></div>\n </div>\n \n <div style=\"display: flex; gap: 1rem; flex-direction: column;\">\n \n <div>\n Use: {{ lightBk }} for '{{ HEX }}' background-color<br>\n <div style=\"padding: 1rem;\" [style.backgroundColor]=\"HEX\" [style.color]=\"darkBk\">\n Sample Text Color\n </div>\n </div>\n\n <div>\n Use: {{ lightBk }} for 'whitesmoke' background-color<br>\n <div style=\"padding: 1rem; background-color: whitesmoke;\" [style.color]=\"lightBk\">\n Sample Text Color\n </div>\n </div>\n </div>\n </div>\n\n <div style=\"margin-top: 1rem; margin-bottom: 1rem;\">\n <mat-divider></mat-divider>\n </div>\n \n <h1>Color Schema Services</h1>\n\n <div style=\"display: flex; gap: 1rem\">\n Pick Color: {{ colorPick }}<br>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"colorPick\"></div>\n Lighter Version: {{ colorPickLighter }}<br>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"colorPickLighter\"></div>\n DarkerVersion: {{ colorPickDarker }}<br>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"colorPickDarker\"></div>\n </div>\n\n <div style=\"margin-top: 1rem; margin-bottom: 1rem;\">\n <mat-divider></mat-divider>\n </div>\n\n <h1>Color Pallette Service</h1>\n Creates Pallette from Image\n <div style=\"display: flex; gap: 2rem;\">\n <div>\n <img [src]=\"img\" height=\"180\">\n </div>\n\n <div style=\"display: flex; gap: .5rem; width: 120px; border: 1px solid black; flex-wrap: wrap; padding: .5rem;\">\n <div>Color Pick</div>\n <ng-container *ngFor=\"let color of (colors$ | async)\">\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"color.color\"></div>\n </ng-container>\n </div>\n \n <div style=\"display: flex; gap: .5rem; width: 120px; border: 1px solid black; flex-wrap: wrap; padding: .5rem;\">\n <div>Complementary</div>\n <ng-container *ngFor=\"let color of (colors$ | async)\">\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"color.complementaryColor\"></div>\n </ng-container>\n </div>\n </div>\n\n</div>\n\n", styles: [".box{width:100px;height:100px;border:solid thin black;color:#000;margin:4px;padding:16px;display:flex;flex-wrap:wrap;align-content:center;justify-content:center}\n"], dependencies: [{ kind: "directive", type: i1.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { kind: "component", type: i2.MatDivider, selector: "mat-divider", inputs: ["vertical", "inset"] }, { kind: "pipe", type: i1.AsyncPipe, name: "async" }] }); }
530
- }
531
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorUtilitiesDemoComponent, decorators: [{
532
- type: Component,
533
- args: [{ selector: 'app-color-utilities-demo', template: "<div style=\"margin: 2rem;\">\n\n <h1>Color Conversion Service</h1>\n <div style=\"display: flex; flex-direction: column; gap: 1rem;\">\n <div style=\"display: flex\">\n <div style=\"padding-top: .5rem; margin-right: .5rem;\">rgbToHex: {{ HEX }}</div>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"HEX\"></div>\n </div>\n\n <div style=\"display: flex\">\n <div style=\"padding-top: .5rem; margin-right: .5rem;\"> hexToRgb: {{ RGB }} </div>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"RGB\"></div>\n </div>\n </div>\n\n <div style=\"margin-top: 1rem; margin-bottom: 1rem;\">\n <mat-divider></mat-divider>\n </div>\n\n <h1>Color Light/Darken Service</h1>\n\n <div style=\"display: flex; flex-direction: column; gap: 1rem;\">\n\n <div style=\"display: flex; gap: 1rem\">\n Original Color: #AA11BB<br>\n <div style=\"width: 32px; height: 32px; background-color: #AA11BB;\"></div>\n Lighten (25%): {{ lighten }}<br>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"lighten\"></div>\n Darken (25%): {{ darken }}<br>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"darken\"></div>\n </div>\n\n </div>\n\n <div style=\"margin-top: 1rem; margin-bottom: 1rem;\">\n <mat-divider></mat-divider>\n </div>\n\n <h1>Text Color Utility Services</h1>\n\n <div style=\"display: flex; gap: 1rem; flex-direction: column;\">\n <div style=\"display: flex; gap: 1rem\">\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"darken\"></div>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"lighten\"></div>\n is Darker : {{ colorIsDarker }}\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"colorIsDarker\"></div>\n </div>\n \n <div style=\"display: flex; gap: 1rem; flex-direction: column;\">\n \n <div>\n Use: {{ lightBk }} for '{{ HEX }}' background-color<br>\n <div style=\"padding: 1rem;\" [style.backgroundColor]=\"HEX\" [style.color]=\"darkBk\">\n Sample Text Color\n </div>\n </div>\n\n <div>\n Use: {{ lightBk }} for 'whitesmoke' background-color<br>\n <div style=\"padding: 1rem; background-color: whitesmoke;\" [style.color]=\"lightBk\">\n Sample Text Color\n </div>\n </div>\n </div>\n </div>\n\n <div style=\"margin-top: 1rem; margin-bottom: 1rem;\">\n <mat-divider></mat-divider>\n </div>\n \n <h1>Color Schema Services</h1>\n\n <div style=\"display: flex; gap: 1rem\">\n Pick Color: {{ colorPick }}<br>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"colorPick\"></div>\n Lighter Version: {{ colorPickLighter }}<br>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"colorPickLighter\"></div>\n DarkerVersion: {{ colorPickDarker }}<br>\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"colorPickDarker\"></div>\n </div>\n\n <div style=\"margin-top: 1rem; margin-bottom: 1rem;\">\n <mat-divider></mat-divider>\n </div>\n\n <h1>Color Pallette Service</h1>\n Creates Pallette from Image\n <div style=\"display: flex; gap: 2rem;\">\n <div>\n <img [src]=\"img\" height=\"180\">\n </div>\n\n <div style=\"display: flex; gap: .5rem; width: 120px; border: 1px solid black; flex-wrap: wrap; padding: .5rem;\">\n <div>Color Pick</div>\n <ng-container *ngFor=\"let color of (colors$ | async)\">\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"color.color\"></div>\n </ng-container>\n </div>\n \n <div style=\"display: flex; gap: .5rem; width: 120px; border: 1px solid black; flex-wrap: wrap; padding: .5rem;\">\n <div>Complementary</div>\n <ng-container *ngFor=\"let color of (colors$ | async)\">\n <div style=\"width: 32px; height: 32px;\" [style.backgroundColor]=\"color.complementaryColor\"></div>\n </ng-container>\n </div>\n </div>\n\n</div>\n\n", styles: [".box{width:100px;height:100px;border:solid thin black;color:#000;margin:4px;padding:16px;display:flex;flex-wrap:wrap;align-content:center;justify-content:center}\n"] }]
534
- }], ctorParameters: function () { return []; } });
535
-
536
- class ColorExtractorDirective {
537
- constructor(elementRef, renderer) {
538
- this.elementRef = elementRef;
539
- this.renderer = renderer;
540
- this.colorValue = new EventEmitter();
541
- }
542
- get currentElement() {
543
- return window.getComputedStyle(this.elementRef.nativeElement);
544
- }
545
- onMouseEnter() {
546
- // console.log('ENTER', this.currentElement)
547
- this.colorValue.emit(this.currentElement.getPropertyValue('background-color'));
548
- }
549
- onMouseLeave() {
550
- // console.log('LEAVE', this.currentElement.getPropertyValue('background-color'))
551
- this.colorValue.emit('white');
552
- }
553
- static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorExtractorDirective, deps: [{ token: i0.ElementRef }, { token: i0.Renderer2 }], target: i0.ɵɵFactoryTarget.Directive }); }
554
- static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.2.12", type: ColorExtractorDirective, selector: "[getColor]", outputs: { colorValue: "colorValue" }, host: { listeners: { "mouseenter": "onMouseEnter()", "mouseleave": "onMouseLeave()" } }, ngImport: i0 }); }
555
- }
556
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorExtractorDirective, decorators: [{
557
- type: Directive,
558
- args: [{
559
- selector: '[getColor]'
560
- }]
561
- }], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i0.Renderer2 }]; }, propDecorators: { colorValue: [{
562
- type: Output
563
- }], onMouseEnter: [{
564
- type: HostListener,
565
- args: ['mouseenter']
566
- }], onMouseLeave: [{
567
- type: HostListener,
568
- args: ['mouseleave']
569
- }] } });
570
-
571
- /*
572
- * Public API Surface of color-utils
573
- */
574
-
575
- /**
576
- * Generated bundle index. Do not edit.
577
- */
578
-
579
- export { ColorConversionService, ColorExtractorDirective, ColorLightenDarkenService, ColorPalletteService, ColorSchemeService, ColorUtilitiesDemoComponent, ColorUtilitiesModule, TextColorService };
580
- //# sourceMappingURL=color-util-helpers.mjs.map