color-util-helpers 1.0.2 → 1.0.4
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.
- package/README.md +63 -4
- package/color-util-helpers-1.0.4.tgz +0 -0
- package/esm2022/lib/color-lighten-darken.service.mjs +79 -0
- package/esm2022/lib/color-pallette.service.mjs +24 -9
- package/esm2022/lib/color-scheme.service.mjs +113 -0
- package/esm2022/lib/color-utilities-demo/color-utilities-demo.component.mjs +41 -0
- package/esm2022/lib/color-utils.module.mjs +32 -0
- package/esm2022/lib/text-color.service.mjs +32 -11
- package/esm2022/public-api.mjs +5 -1
- package/fesm2022/color-util-helpers.mjs +341 -55
- package/fesm2022/color-util-helpers.mjs.map +1 -1
- package/lib/color-lighten-darken.service.d.ts +10 -0
- package/lib/color-pallette.service.d.ts +18 -0
- package/lib/color-scheme.service.d.ts +45 -0
- package/lib/color-utilities-demo/color-utilities-demo.component.d.ts +34 -0
- package/lib/color-utils.module.d.ts +10 -0
- package/lib/text-color.service.d.ts +3 -0
- package/package.json +1 -1
- package/public-api.d.ts +4 -0
- package/color-util-helpers-1.0.2.tgz +0 -0
|
@@ -13,14 +13,14 @@ export class TextColorService {
|
|
|
13
13
|
return ((r * 0.299 + g * 0.587 + b * 0.114) > 149) ? darkColor : lightColor;
|
|
14
14
|
}
|
|
15
15
|
darkerColor(color1, color2) {
|
|
16
|
-
return
|
|
16
|
+
return this.isColorDarker(color1, color2) ? color1 : color2;
|
|
17
17
|
}
|
|
18
18
|
isColorDarker(color1, color2) {
|
|
19
19
|
const newColor1 = this.fixColor(color1);
|
|
20
20
|
const newColor2 = this.fixColor(color2);
|
|
21
21
|
const luminance1 = this.calculateLuminance(newColor1[0], newColor1[1], newColor1[2]);
|
|
22
22
|
const luminance2 = this.calculateLuminance(newColor2[0], newColor2[1], newColor2[2]);
|
|
23
|
-
return
|
|
23
|
+
return luminance1 < luminance2;
|
|
24
24
|
}
|
|
25
25
|
lighterColor(color1, color2) {
|
|
26
26
|
return (this.isColorLighter(color1, color2)) ? color1 : color2;
|
|
@@ -36,15 +36,36 @@ export class TextColorService {
|
|
|
36
36
|
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
37
37
|
}
|
|
38
38
|
fixColor(color) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
39
|
+
// Remove leading hash if present
|
|
40
|
+
const sanitizedColor = color.startsWith('#') ? color.slice(1) : color;
|
|
41
|
+
// Validate if the color is a valid hex (3 or 6 characters)
|
|
42
|
+
if (!this.isValidHex(sanitizedColor)) {
|
|
43
|
+
return this.parseRgb(sanitizedColor); // If not hex, attempt to parse as RGB
|
|
43
44
|
}
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
// Convert hex to RGB
|
|
46
|
+
const rgb = this.hexToRgb(sanitizedColor);
|
|
47
|
+
return rgb;
|
|
48
|
+
}
|
|
49
|
+
// Helper function to validate if a string is a valid 3 or 6 digit hex code
|
|
50
|
+
isValidHex(color) {
|
|
51
|
+
const hexRegex = /^[A-Fa-f0-9]{3}$|^[A-Fa-f0-9]{6}$/i;
|
|
52
|
+
return hexRegex.test(color);
|
|
53
|
+
}
|
|
54
|
+
// Helper function to convert a 3 or 6 digit hex color to RGB
|
|
55
|
+
hexToRgb(hex) {
|
|
56
|
+
if (hex.length === 3) {
|
|
57
|
+
hex = hex.split('').map(c => c + c).join(''); // Expand shorthand hex to full
|
|
58
|
+
}
|
|
59
|
+
return [
|
|
60
|
+
parseInt(hex.slice(0, 2), 16),
|
|
61
|
+
parseInt(hex.slice(2, 4), 16),
|
|
62
|
+
parseInt(hex.slice(4, 6), 16),
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
// Helper function to parse an RGB string (e.g., rgb(255, 0, 0)) into an array
|
|
66
|
+
parseRgb(rgb) {
|
|
67
|
+
const match = rgb.match(/\d+/g);
|
|
68
|
+
return match ? match.map(num => parseInt(num)) : [];
|
|
48
69
|
}
|
|
49
70
|
static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TextColorService, deps: [{ token: i1.ColorConversionService }], target: i0.ɵɵFactoryTarget.Injectable }); }
|
|
50
71
|
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TextColorService, providedIn: 'root' }); }
|
|
@@ -55,4 +76,4 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImpo
|
|
|
55
76
|
providedIn: 'root'
|
|
56
77
|
}]
|
|
57
78
|
}], ctorParameters: function () { return [{ type: i1.ColorConversionService }]; } });
|
|
58
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
79
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGV4dC1jb2xvci5zZXJ2aWNlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vcHJvamVjdHMvY29sb3ItdXRpbHMvc3JjL2xpYi90ZXh0LWNvbG9yLnNlcnZpY2UudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLFVBQVUsRUFBRSxNQUFNLGVBQWUsQ0FBQTs7O0FBTTFDLE1BQU0sT0FBTyxnQkFBZ0I7SUFFM0IsWUFDVSxzQkFBOEM7UUFBOUMsMkJBQXNCLEdBQXRCLHNCQUFzQixDQUF3QjtJQUNyRCxDQUFDO0lBRUosbUJBQW1CLENBQUMsT0FBZSxFQUFFLFVBQWtCLEVBQUUsU0FBaUI7UUFFeEUsTUFBTSxRQUFRLEdBQUcsSUFBSSxDQUFDLFFBQVEsQ0FBQyxPQUFPLENBQUMsQ0FBQTtRQUV2QyxNQUFNLENBQUMsR0FBRyxRQUFRLENBQUMsQ0FBQyxDQUFDLENBQUE7UUFDckIsTUFBTSxDQUFDLEdBQUcs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nVsbFxyXG4gICAgfVxyXG5cclxuICAgIHJldHVybiBbXHJcbiAgICAgIHBhcnNlSW50KGhleC5zbGljZSgwLCAyKSwgMTYpLFxyXG4gICAgICBwYXJzZUludChoZXguc2xpY2UoMiwgNCksIDE2KSxcclxuICAgICAgcGFyc2VJbnQoaGV4LnNsaWNlKDQsIDYpLCAxNiksXHJcbiAgICBdO1xyXG4gIH1cclxuXHJcbiAgLy8gSGVscGVyIGZ1bmN0aW9uIHRvIHBhcnNlIGFuIFJHQiBzdHJpbmcgKGUuZy4sIHJnYigyNTUsIDAsIDApKSBpbnRvIGFuIGFycmF5XHJcbiAgcGFyc2VSZ2IocmdiOiBzdHJpbmcpOiBudW1iZXJbXSB7XHJcbiAgICBjb25zdCBtYXRjaCA9IHJnYi5tYXRjaCgvXFxkKy9nKTtcclxuICAgIHJldHVybiBtYXRjaCA/IG1hdGNoLm1hcChudW0gPT4gcGFyc2VJbnQobnVtKSkgOiBbXTtcclxuICB9XHJcblxyXG5cclxufVxyXG4iXX0=
|
package/esm2022/public-api.mjs
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* Public API Surface of color-utils
|
|
3
3
|
*/
|
|
4
|
+
export * from './lib/color-utils.module';
|
|
5
|
+
export * from './lib/color-utilities-demo/color-utilities-demo.component';
|
|
4
6
|
export * from './lib/color-conversion.service';
|
|
5
7
|
export * from './lib/color-extractor.directive';
|
|
6
8
|
export * from './lib/color-pallette.service';
|
|
7
9
|
export * from './lib/text-color.service';
|
|
8
|
-
|
|
10
|
+
export * from './lib/color-scheme.service';
|
|
11
|
+
export * from './lib/color-lighten-darken.service';
|
|
12
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHVibGljLWFwaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3Byb2plY3RzL2NvbG9yLXV0aWxzL3NyYy9wdWJsaWMtYXBpLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOztHQUVHO0FBRUgsY0FBYywwQkFBMEIsQ0FBQztBQUN6QyxjQUFjLDJEQUEyRCxDQUFDO0FBRTFFLGNBQWMsZ0NBQWdDLENBQUM7QUFDL0MsY0FBYyxpQ0FBaUMsQ0FBQztBQUNoRCxjQUFjLDhCQUE4QixDQUFDO0FBQzdDLGNBQWMsMEJBQTBCLENBQUM7QUFFekMsY0FBYyw0QkFBNEIsQ0FBQztBQUMzQyxjQUFjLG9DQUFvQyxDQUFDIiwic291cmNlc0NvbnRlbnQiOlsiLypcbiAqIFB1YmxpYyBBUEkgU3VyZmFjZSBvZiBjb2xvci11dGlsc1xuICovXG5cbmV4cG9ydCAqIGZyb20gJy4vbGliL2NvbG9yLXV0aWxzLm1vZHVsZSc7XG5leHBvcnQgKiBmcm9tICcuL2xpYi9jb2xvci11dGlsaXRpZXMtZGVtby9jb2xvci11dGlsaXRpZXMtZGVtby5jb21wb25lbnQnO1xuXG5leHBvcnQgKiBmcm9tICcuL2xpYi9jb2xvci1jb252ZXJzaW9uLnNlcnZpY2UnO1xuZXhwb3J0ICogZnJvbSAnLi9saWIvY29sb3ItZXh0cmFjdG9yLmRpcmVjdGl2ZSc7XG5leHBvcnQgKiBmcm9tICcuL2xpYi9jb2xvci1wYWxsZXR0ZS5zZXJ2aWNlJztcbmV4cG9ydCAqIGZyb20gJy4vbGliL3RleHQtY29sb3Iuc2VydmljZSc7XG5cbmV4cG9ydCAqIGZyb20gJy4vbGliL2NvbG9yLXNjaGVtZS5zZXJ2aWNlJztcbmV4cG9ydCAqIGZyb20gJy4vbGliL2NvbG9yLWxpZ2h0ZW4tZGFya2VuLnNlcnZpY2UnO1xuIl19
|
|
@@ -1,7 +1,38 @@
|
|
|
1
1
|
import * as i0 from '@angular/core';
|
|
2
|
-
import { Injectable, EventEmitter, Directive, Output, HostListener } 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';
|
|
3
8
|
import { BehaviorSubject } from 'rxjs';
|
|
4
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
|
+
|
|
5
36
|
class ColorConversionService {
|
|
6
37
|
constructor() {
|
|
7
38
|
this.componentToHex = (c) => {
|
|
@@ -39,41 +70,6 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImpo
|
|
|
39
70
|
}]
|
|
40
71
|
}] });
|
|
41
72
|
|
|
42
|
-
class ColorExtractorDirective {
|
|
43
|
-
constructor(elementRef, renderer) {
|
|
44
|
-
this.elementRef = elementRef;
|
|
45
|
-
this.renderer = renderer;
|
|
46
|
-
this.colorValue = new EventEmitter();
|
|
47
|
-
}
|
|
48
|
-
get currentElement() {
|
|
49
|
-
return window.getComputedStyle(this.elementRef.nativeElement);
|
|
50
|
-
}
|
|
51
|
-
onMouseEnter() {
|
|
52
|
-
// console.log('ENTER', this.currentElement)
|
|
53
|
-
this.colorValue.emit(this.currentElement.getPropertyValue('background-color'));
|
|
54
|
-
}
|
|
55
|
-
onMouseLeave() {
|
|
56
|
-
// console.log('LEAVE', this.currentElement.getPropertyValue('background-color'))
|
|
57
|
-
this.colorValue.emit('white');
|
|
58
|
-
}
|
|
59
|
-
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 }); }
|
|
60
|
-
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 }); }
|
|
61
|
-
}
|
|
62
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: ColorExtractorDirective, decorators: [{
|
|
63
|
-
type: Directive,
|
|
64
|
-
args: [{
|
|
65
|
-
selector: '[getColor]'
|
|
66
|
-
}]
|
|
67
|
-
}], ctorParameters: function () { return [{ type: i0.ElementRef }, { type: i0.Renderer2 }]; }, propDecorators: { colorValue: [{
|
|
68
|
-
type: Output
|
|
69
|
-
}], onMouseEnter: [{
|
|
70
|
-
type: HostListener,
|
|
71
|
-
args: ['mouseenter']
|
|
72
|
-
}], onMouseLeave: [{
|
|
73
|
-
type: HostListener,
|
|
74
|
-
args: ['mouseleave']
|
|
75
|
-
}] } });
|
|
76
|
-
|
|
77
73
|
class ColorPalletteService {
|
|
78
74
|
constructor(colorConversionService) {
|
|
79
75
|
this.colorConversionService = colorConversionService;
|
|
@@ -112,7 +108,15 @@ class ColorPalletteService {
|
|
|
112
108
|
// justify-content: center;
|
|
113
109
|
// }
|
|
114
110
|
this.palette = new BehaviorSubject([]);
|
|
111
|
+
this.palette$ = this.palette.asObservable();
|
|
115
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
|
+
*/
|
|
116
120
|
getColorsFromImage(imagePath, colors = 3) {
|
|
117
121
|
const image = new Image();
|
|
118
122
|
image.src = imagePath;
|
|
@@ -121,10 +125,17 @@ class ColorPalletteService {
|
|
|
121
125
|
this.palette.next(data);
|
|
122
126
|
};
|
|
123
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
|
+
*/
|
|
124
135
|
generateColorPalette(image, colorCount = 6) {
|
|
125
|
-
const canvas = document.createElement(
|
|
126
|
-
const context = canvas.getContext(
|
|
127
|
-
if (!context
|
|
136
|
+
const canvas = document.createElement("canvas");
|
|
137
|
+
const context = canvas.getContext("2d");
|
|
138
|
+
if (!context)
|
|
128
139
|
return;
|
|
129
140
|
canvas.width = image.width;
|
|
130
141
|
canvas.height = image.height;
|
|
@@ -150,8 +161,8 @@ class ColorPalletteService {
|
|
|
150
161
|
const luminance2 = this.getLuminance(color2);
|
|
151
162
|
return luminance2 - luminance1;
|
|
152
163
|
});
|
|
153
|
-
const palette = quantizedColors.map(color => {
|
|
154
|
-
const complementaryColor = color.map(component => 255 - component);
|
|
164
|
+
const palette = quantizedColors.map((color) => {
|
|
165
|
+
const complementaryColor = color.map((component) => 255 - component);
|
|
155
166
|
const hexColor = this.colorConversionService.rgbToHex(color);
|
|
156
167
|
const hexComplementaryColor = this.colorConversionService.rgbToHex(complementaryColor);
|
|
157
168
|
return { color: hexColor, complementaryColor: hexComplementaryColor };
|
|
@@ -186,12 +197,12 @@ class ColorPalletteService {
|
|
|
186
197
|
return [meanR, meanG, meanB];
|
|
187
198
|
}
|
|
188
199
|
kMeansColorQuantization(colors, k) {
|
|
189
|
-
|
|
200
|
+
let clusterCenters = [];
|
|
190
201
|
for (let i = 0; i < k; i++) {
|
|
191
202
|
const randomColor = colors[Math.floor(Math.random() * colors.length)];
|
|
192
203
|
clusterCenters.push(randomColor);
|
|
193
204
|
}
|
|
194
|
-
|
|
205
|
+
let clusters = [];
|
|
195
206
|
for (let i = 0; i < colors.length; i++) {
|
|
196
207
|
const color = colors[i];
|
|
197
208
|
let minDistance = Infinity;
|
|
@@ -206,7 +217,7 @@ class ColorPalletteService {
|
|
|
206
217
|
}
|
|
207
218
|
clusters.push({ color, center: nearestCenter });
|
|
208
219
|
}
|
|
209
|
-
|
|
220
|
+
let updatedCenters = [];
|
|
210
221
|
for (let i = 0; i < clusterCenters.length; i++) {
|
|
211
222
|
const center = clusterCenters[i];
|
|
212
223
|
const clusterColors = clusters.filter(c => c.center === center).map(c => c.color);
|
|
@@ -239,14 +250,14 @@ class TextColorService {
|
|
|
239
250
|
return ((r * 0.299 + g * 0.587 + b * 0.114) > 149) ? darkColor : lightColor;
|
|
240
251
|
}
|
|
241
252
|
darkerColor(color1, color2) {
|
|
242
|
-
return
|
|
253
|
+
return this.isColorDarker(color1, color2) ? color1 : color2;
|
|
243
254
|
}
|
|
244
255
|
isColorDarker(color1, color2) {
|
|
245
256
|
const newColor1 = this.fixColor(color1);
|
|
246
257
|
const newColor2 = this.fixColor(color2);
|
|
247
258
|
const luminance1 = this.calculateLuminance(newColor1[0], newColor1[1], newColor1[2]);
|
|
248
259
|
const luminance2 = this.calculateLuminance(newColor2[0], newColor2[1], newColor2[2]);
|
|
249
|
-
return
|
|
260
|
+
return luminance1 < luminance2;
|
|
250
261
|
}
|
|
251
262
|
lighterColor(color1, color2) {
|
|
252
263
|
return (this.isColorLighter(color1, color2)) ? color1 : color2;
|
|
@@ -262,15 +273,36 @@ class TextColorService {
|
|
|
262
273
|
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
263
274
|
}
|
|
264
275
|
fixColor(color) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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
|
|
269
295
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
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)) : [];
|
|
274
306
|
}
|
|
275
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 }); }
|
|
276
308
|
static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TextColorService, providedIn: 'root' }); }
|
|
@@ -282,6 +314,260 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImpo
|
|
|
282
314
|
}]
|
|
283
315
|
}], ctorParameters: function () { return [{ type: ColorConversionService }]; } });
|
|
284
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
|
+
|
|
285
571
|
/*
|
|
286
572
|
* Public API Surface of color-utils
|
|
287
573
|
*/
|
|
@@ -290,5 +576,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImpo
|
|
|
290
576
|
* Generated bundle index. Do not edit.
|
|
291
577
|
*/
|
|
292
578
|
|
|
293
|
-
export { ColorConversionService, ColorExtractorDirective, ColorPalletteService, TextColorService };
|
|
579
|
+
export { ColorConversionService, ColorExtractorDirective, ColorLightenDarkenService, ColorPalletteService, ColorSchemeService, ColorUtilitiesDemoComponent, ColorUtilitiesModule, TextColorService };
|
|
294
580
|
//# sourceMappingURL=color-util-helpers.mjs.map
|