color-util-helpers 1.0.7 → 1.0.10
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 +6 -2
- package/color-util-helpers-1.0.10.tgz +0 -0
- package/esm2022/color-util-helpers.mjs +5 -0
- package/esm2022/lib/color-conversion.service.mjs +39 -0
- package/esm2022/lib/color-extractor.directive.mjs +37 -0
- package/esm2022/lib/color-grab.directive.mjs +185 -0
- package/esm2022/lib/color-lighten-darken.service.mjs +79 -0
- package/esm2022/lib/color-pallette.service.mjs +172 -0
- 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 +38 -0
- package/esm2022/lib/text-color.service.mjs +79 -0
- package/esm2022/public-api.mjs +13 -0
- package/fesm2022/color-util-helpers.mjs +767 -0
- package/fesm2022/color-util-helpers.mjs.map +1 -0
- package/index.d.ts +5 -0
- package/lib/color-conversion.service.d.ts +8 -0
- package/lib/color-extractor.directive.d.ts +13 -0
- package/lib/color-grab.directive.d.ts +31 -0
- package/lib/color-lighten-darken.service.d.ts +10 -0
- package/lib/color-pallette.service.d.ts +36 -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 +12 -0
- package/lib/text-color.service.d.ts +18 -0
- package/package.json +15 -2
- package/{src/public-api.ts → public-api.d.ts} +0 -6
- package/ng-package.json +0 -8
- package/src/lib/assets/picture.webp +0 -0
- package/src/lib/color-conversion.service.spec.ts +0 -54
- package/src/lib/color-conversion.service.ts +0 -35
- package/src/lib/color-extractor.directive.spec.ts +0 -49
- package/src/lib/color-extractor.directive.ts +0 -28
- package/src/lib/color-grab.directive.ts +0 -204
- package/src/lib/color-lighten-darken.service.spec.ts +0 -61
- package/src/lib/color-lighten-darken.service.ts +0 -83
- package/src/lib/color-pallette.service.spec.ts +0 -85
- package/src/lib/color-pallette.service.ts +0 -191
- package/src/lib/color-scheme.service.ts +0 -123
- package/src/lib/color-utilities-demo/color-utilities-demo.component.css +0 -12
- package/src/lib/color-utilities-demo/color-utilities-demo.component.html +0 -109
- package/src/lib/color-utilities-demo/color-utilities-demo.component.ts +0 -57
- package/src/lib/color-utils.module.ts +0 -27
- package/src/lib/text-color.service.spec.ts +0 -75
- package/src/lib/text-color.service.ts +0 -101
- package/tsconfig.lib.json +0 -32
- package/tsconfig.lib.prod.json +0 -10
- package/tsconfig.spec.json +0 -14
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@angular/core';
|
|
2
|
-
|
|
3
|
-
@Injectable({
|
|
4
|
-
providedIn: 'root'
|
|
5
|
-
})
|
|
6
|
-
export class ColorSchemeService {
|
|
7
|
-
|
|
8
|
-
constructor() { }
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Generates a random hexadecimal color code.
|
|
12
|
-
*
|
|
13
|
-
* 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.
|
|
14
|
-
*
|
|
15
|
-
* @returns A hexadecimal color code in the format "#RRGGBB".
|
|
16
|
-
*/
|
|
17
|
-
generateRandomColor() {
|
|
18
|
-
// Generate a random hue between 0 and 360 (representing degrees on the color wheel)
|
|
19
|
-
const hue = Math.floor(Math.random() * 360);
|
|
20
|
-
|
|
21
|
-
// Generate random saturation and lightness values between 50% and 100%
|
|
22
|
-
const saturation = Math.floor(Math.random() * 51) + 50;
|
|
23
|
-
const lightness = Math.floor(Math.random() * 51) + 50;
|
|
24
|
-
|
|
25
|
-
// Convert HSL values to RGB values
|
|
26
|
-
const rgbColor = this.hslToRgb(hue, saturation, lightness);
|
|
27
|
-
|
|
28
|
-
// Convert RGB values to hexadecimal color code
|
|
29
|
-
const hexColor = this.rgbToHex(rgbColor.r, rgbColor.g, rgbColor.b);
|
|
30
|
-
|
|
31
|
-
return hexColor;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Converts HSL (Hue, Saturation, Lightness) color values to RGB (Red, Green, Blue) color values.
|
|
36
|
-
*
|
|
37
|
-
* @param h - The hue value, ranging from 0 to 360 degrees.
|
|
38
|
-
* @param s - The saturation value, ranging from 0 to 100 percent.
|
|
39
|
-
* @param l - The lightness value, ranging from 0 to 100 percent.
|
|
40
|
-
* @returns An object with the RGB color values, where each value is between 0 and 255.
|
|
41
|
-
*/
|
|
42
|
-
hslToRgb(h: number, s: number, l: number) {
|
|
43
|
-
h /= 360;
|
|
44
|
-
s /= 100;
|
|
45
|
-
l /= 100;
|
|
46
|
-
|
|
47
|
-
let r, g, b;
|
|
48
|
-
|
|
49
|
-
if (s === 0) {
|
|
50
|
-
r = g = b = l; // Achromatic color (gray)
|
|
51
|
-
} else {
|
|
52
|
-
const hueToRgb = (p: number, q: number, t: number) => {
|
|
53
|
-
if (t < 0) t += 1;
|
|
54
|
-
if (t > 1) t -= 1;
|
|
55
|
-
if (t < 1 / 6) return p + (q - p) * 6 * t;
|
|
56
|
-
if (t < 1 / 2) return q;
|
|
57
|
-
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
|
|
58
|
-
return p;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
62
|
-
const p = 2 * l - q;
|
|
63
|
-
|
|
64
|
-
r = Math.round(hueToRgb(p, q, h + 1 / 3) * 255);
|
|
65
|
-
g = Math.round(hueToRgb(p, q, h) * 255);
|
|
66
|
-
b = Math.round(hueToRgb(p, q, h - 1 / 3) * 255);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
return { r, g, b };
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Converts RGB color values to a hexadecimal color string.
|
|
74
|
-
*
|
|
75
|
-
* @param r - The red color value, between 0 and 255.
|
|
76
|
-
* @param g - The green color value, between 0 and 255.
|
|
77
|
-
* @param b - The blue color value, between 0 and 255.
|
|
78
|
-
* @returns A hexadecimal color string in the format "#RRGGBB".
|
|
79
|
-
*/
|
|
80
|
-
rgbToHex(r: number, g: number, b: number) {
|
|
81
|
-
const componentToHex = (c: number) => {
|
|
82
|
-
const hex = c.toString(16);
|
|
83
|
-
return hex.length === 1 ? "0" + hex : hex;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Adjusts a hexadecimal color value by a given percentage.
|
|
91
|
-
*
|
|
92
|
-
* @param hexColor - The hexadecimal color value to adjust.
|
|
93
|
-
* @param percentage - The percentage to adjust the color by, ranging from -100 to 100.
|
|
94
|
-
* @returns The adjusted hexadecimal color value.
|
|
95
|
-
*/
|
|
96
|
-
adjustHexColor(hexColor: string, percentage: number) {
|
|
97
|
-
// Remove the "#" symbol if present
|
|
98
|
-
hexColor = hexColor.replace("#", "");
|
|
99
|
-
|
|
100
|
-
// Convert the hex color to RGB values
|
|
101
|
-
const red = parseInt(hexColor.substring(0, 2), 16);
|
|
102
|
-
const green = parseInt(hexColor.substring(2, 4), 16);
|
|
103
|
-
const blue = parseInt(hexColor.substring(4, 6), 16);
|
|
104
|
-
|
|
105
|
-
// Calculate the adjustment amount based on the percentage
|
|
106
|
-
const adjustAmount = Math.round(255 * (percentage / 100));
|
|
107
|
-
|
|
108
|
-
// Adjust the RGB values
|
|
109
|
-
const adjustedRed = this.clamp(red + adjustAmount);
|
|
110
|
-
const adjustedGreen = this.clamp(green + adjustAmount);
|
|
111
|
-
const adjustedBlue = this.clamp(blue + adjustAmount);
|
|
112
|
-
|
|
113
|
-
// Convert the adjusted RGB values back to hex
|
|
114
|
-
const adjustedHexColor = `#${(adjustedRed).toString(16).padStart(2, '0')}${(adjustedGreen).toString(16).padStart(2, '0')}${(adjustedBlue).toString(16).padStart(2, '0')}`;
|
|
115
|
-
|
|
116
|
-
return adjustedHexColor;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
clamp(value: number) {
|
|
120
|
-
return Math.max(0, Math.min(value, 255));
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
<div style="margin: 2rem;">
|
|
2
|
-
|
|
3
|
-
<h1>Color Conversion Service</h1>
|
|
4
|
-
<div style="display: flex; flex-direction: column; gap: 1rem;">
|
|
5
|
-
<div style="display: flex">
|
|
6
|
-
<div style="padding-top: .5rem; margin-right: .5rem;">rgbToHex: {{ HEX }}</div>
|
|
7
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="HEX"></div>
|
|
8
|
-
</div>
|
|
9
|
-
|
|
10
|
-
<div style="display: flex">
|
|
11
|
-
<div style="padding-top: .5rem; margin-right: .5rem;"> hexToRgb: {{ RGB }} </div>
|
|
12
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="RGB"></div>
|
|
13
|
-
</div>
|
|
14
|
-
</div>
|
|
15
|
-
|
|
16
|
-
<div style="margin-top: 1rem; margin-bottom: 1rem;">
|
|
17
|
-
<mat-divider></mat-divider>
|
|
18
|
-
</div>
|
|
19
|
-
|
|
20
|
-
<h1>Color Light/Darken Service</h1>
|
|
21
|
-
|
|
22
|
-
<div style="display: flex; flex-direction: column; gap: 1rem;">
|
|
23
|
-
|
|
24
|
-
<div style="display: flex; gap: 1rem">
|
|
25
|
-
Original Color: #AA11BB<br>
|
|
26
|
-
<div style="width: 32px; height: 32px; background-color: #AA11BB;"></div>
|
|
27
|
-
Lighten (25%): {{ lighten }}<br>
|
|
28
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="lighten"></div>
|
|
29
|
-
Darken (25%): {{ darken }}<br>
|
|
30
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="darken"></div>
|
|
31
|
-
</div>
|
|
32
|
-
|
|
33
|
-
</div>
|
|
34
|
-
|
|
35
|
-
<div style="margin-top: 1rem; margin-bottom: 1rem;">
|
|
36
|
-
<mat-divider></mat-divider>
|
|
37
|
-
</div>
|
|
38
|
-
|
|
39
|
-
<h1>Text Color Utility Services</h1>
|
|
40
|
-
|
|
41
|
-
<div style="display: flex; gap: 1rem; flex-direction: column;">
|
|
42
|
-
<div style="display: flex; gap: 1rem">
|
|
43
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="darken"></div>
|
|
44
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="lighten"></div>
|
|
45
|
-
is Darker : {{ colorIsDarker }}
|
|
46
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="colorIsDarker"></div>
|
|
47
|
-
</div>
|
|
48
|
-
|
|
49
|
-
<div style="display: flex; gap: 1rem; flex-direction: column;">
|
|
50
|
-
|
|
51
|
-
<div>
|
|
52
|
-
Use: {{ lightBk }} for '{{ HEX }}' background-color<br>
|
|
53
|
-
<div style="padding: 1rem;" [style.backgroundColor]="HEX" [style.color]="darkBk">
|
|
54
|
-
Sample Text Color
|
|
55
|
-
</div>
|
|
56
|
-
</div>
|
|
57
|
-
|
|
58
|
-
<div>
|
|
59
|
-
Use: {{ lightBk }} for 'whitesmoke' background-color<br>
|
|
60
|
-
<div style="padding: 1rem; background-color: whitesmoke;" [style.color]="lightBk">
|
|
61
|
-
Sample Text Color
|
|
62
|
-
</div>
|
|
63
|
-
</div>
|
|
64
|
-
</div>
|
|
65
|
-
</div>
|
|
66
|
-
|
|
67
|
-
<div style="margin-top: 1rem; margin-bottom: 1rem;">
|
|
68
|
-
<mat-divider></mat-divider>
|
|
69
|
-
</div>
|
|
70
|
-
|
|
71
|
-
<h1>Color Schema Services</h1>
|
|
72
|
-
|
|
73
|
-
<div style="display: flex; gap: 1rem">
|
|
74
|
-
Pick Color: {{ colorPick }}<br>
|
|
75
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="colorPick"></div>
|
|
76
|
-
Lighter Version: {{ colorPickLighter }}<br>
|
|
77
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="colorPickLighter"></div>
|
|
78
|
-
DarkerVersion: {{ colorPickDarker }}<br>
|
|
79
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="colorPickDarker"></div>
|
|
80
|
-
</div>
|
|
81
|
-
|
|
82
|
-
<div style="margin-top: 1rem; margin-bottom: 1rem;">
|
|
83
|
-
<mat-divider></mat-divider>
|
|
84
|
-
</div>
|
|
85
|
-
|
|
86
|
-
<h1>Color Pallette Service</h1>
|
|
87
|
-
Creates Pallette from Image
|
|
88
|
-
<div style="display: flex; gap: 2rem;">
|
|
89
|
-
<div>
|
|
90
|
-
<img [src]="img" height="180">
|
|
91
|
-
</div>
|
|
92
|
-
|
|
93
|
-
<div style="display: flex; gap: .5rem; width: 120px; border: 1px solid black; flex-wrap: wrap; padding: .5rem;">
|
|
94
|
-
<div>Color Pick</div>
|
|
95
|
-
<ng-container *ngFor="let color of (colors$ | async)">
|
|
96
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="color.color"></div>
|
|
97
|
-
</ng-container>
|
|
98
|
-
</div>
|
|
99
|
-
|
|
100
|
-
<div style="display: flex; gap: .5rem; width: 120px; border: 1px solid black; flex-wrap: wrap; padding: .5rem;">
|
|
101
|
-
<div>Complementary</div>
|
|
102
|
-
<ng-container *ngFor="let color of (colors$ | async)">
|
|
103
|
-
<div style="width: 32px; height: 32px;" [style.backgroundColor]="color.complementaryColor"></div>
|
|
104
|
-
</ng-container>
|
|
105
|
-
</div>
|
|
106
|
-
</div>
|
|
107
|
-
|
|
108
|
-
</div>
|
|
109
|
-
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { Component, OnInit, inject } from '@angular/core';
|
|
2
|
-
|
|
3
|
-
import { ColorConversionService } from '../color-conversion.service';
|
|
4
|
-
import { ColorPalletteService } from '../color-pallette.service';
|
|
5
|
-
import { TextColorService } from '../text-color.service';
|
|
6
|
-
import { ColorLightenDarkenService } from '../color-lighten-darken.service';
|
|
7
|
-
import { ColorSchemeService } from '../color-scheme.service';
|
|
8
|
-
|
|
9
|
-
@Component({
|
|
10
|
-
selector: 'app-color-utilities-demo',
|
|
11
|
-
templateUrl: './color-utilities-demo.component.html',
|
|
12
|
-
styleUrls: ['./color-utilities-demo.component.css'],
|
|
13
|
-
})
|
|
14
|
-
export class ColorUtilitiesDemoComponent implements OnInit {
|
|
15
|
-
|
|
16
|
-
colorConversionService = inject(ColorConversionService)
|
|
17
|
-
colorLightenDarkenService = inject(ColorLightenDarkenService)
|
|
18
|
-
colorPalletteService = inject(ColorPalletteService)
|
|
19
|
-
textColorService = inject(TextColorService)
|
|
20
|
-
colorSchemeService = inject(ColorSchemeService)
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
HEX = this.colorConversionService.rgbToHex([12, 56, 128])
|
|
24
|
-
RGB = `rgb(${this.colorConversionService.hexToRgb('#AA11BB')})`
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
lighten = this.colorLightenDarkenService.lighten('#AA11BB', .25)
|
|
28
|
-
darken = this.colorLightenDarkenService.darken('#AA11BB', .25)
|
|
29
|
-
|
|
30
|
-
colorIsDarker = this.textColorService.isColorDarker(this.lighten, this.darken)
|
|
31
|
-
|
|
32
|
-
darkBk = this.textColorService.textColorForBgColor(this.HEX, this.lighten, this.darken)
|
|
33
|
-
lightBk = this.textColorService.textColorForBgColor('whitesmoke', this.lighten, this.darken)
|
|
34
|
-
|
|
35
|
-
palette: any
|
|
36
|
-
colors$ = this.colorPalletteService.palette$
|
|
37
|
-
|
|
38
|
-
colorPick = this.colorSchemeService.generateRandomColor()
|
|
39
|
-
colorPickDarker = this.colorSchemeService.adjustHexColor(this.colorPick, -25)
|
|
40
|
-
colorPickLighter = this.colorSchemeService.adjustHexColor(this.colorPick, 25)
|
|
41
|
-
|
|
42
|
-
img: string|any
|
|
43
|
-
|
|
44
|
-
constructor() { }
|
|
45
|
-
|
|
46
|
-
ngOnInit() {
|
|
47
|
-
|
|
48
|
-
// define image path
|
|
49
|
-
this.img = 'assets/picture.webp'
|
|
50
|
-
this.colorPalletteService.getColorsFromImage(this.img, 8)
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { NgModule } from '@angular/core';
|
|
2
|
-
import { CommonModule } from '@angular/common';
|
|
3
|
-
|
|
4
|
-
import { MatButtonModule } from '@angular/material/button';
|
|
5
|
-
import { MatDividerModule } from '@angular/material/divider';
|
|
6
|
-
import { ColorGrabberDirective } from './color-grab.directive';
|
|
7
|
-
|
|
8
|
-
import { ColorUtilitiesDemoComponent } from './color-utilities-demo/color-utilities-demo.component';
|
|
9
|
-
import { ColorExtractorDirective } from './color-extractor.directive';
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
@NgModule({
|
|
13
|
-
imports: [
|
|
14
|
-
CommonModule,
|
|
15
|
-
MatButtonModule,
|
|
16
|
-
MatDividerModule
|
|
17
|
-
],
|
|
18
|
-
declarations: [
|
|
19
|
-
ColorUtilitiesDemoComponent,
|
|
20
|
-
ColorGrabberDirective,
|
|
21
|
-
ColorExtractorDirective
|
|
22
|
-
],
|
|
23
|
-
exports: [
|
|
24
|
-
ColorUtilitiesDemoComponent
|
|
25
|
-
]
|
|
26
|
-
})
|
|
27
|
-
export class ColorUtilitiesModule { }
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { TestBed } from '@angular/core/testing';
|
|
2
|
-
import { TextColorService } from './text-color.service';
|
|
3
|
-
import { ColorConversionService } from './color-conversion.service';
|
|
4
|
-
|
|
5
|
-
describe('TextColorService', () => {
|
|
6
|
-
let service: TextColorService;
|
|
7
|
-
let colorConversionServiceSpy: jasmine.SpyObj<ColorConversionService>;
|
|
8
|
-
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
const spy = jasmine.createSpyObj('ColorConversionService', ['hexToRgb']);
|
|
11
|
-
|
|
12
|
-
TestBed.configureTestingModule({
|
|
13
|
-
providers: [
|
|
14
|
-
TextColorService,
|
|
15
|
-
{ provide: ColorConversionService, useValue: spy }
|
|
16
|
-
]
|
|
17
|
-
});
|
|
18
|
-
service = TestBed.inject(TextColorService);
|
|
19
|
-
colorConversionServiceSpy = TestBed.inject(ColorConversionService) as jasmine.SpyObj<ColorConversionService>;
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
it('should be created', () => {
|
|
23
|
-
expect(service).toBeTruthy();
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('should return dark text for light background', () => {
|
|
27
|
-
colorConversionServiceSpy.hexToRgb.and.returnValue([255, 255, 255]); // White
|
|
28
|
-
expect(service.textColorForBgColor('#FFFFFF', '#FFFFFF', '#000000')).toBe('#000000');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('should return light text for dark background', () => {
|
|
32
|
-
colorConversionServiceSpy.hexToRgb.and.returnValue([0, 0, 0]); // Black
|
|
33
|
-
expect(service.textColorForBgColor('#000000', '#FFFFFF', '#000000')).toBe('#FFFFFF');
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should determine darker color correctly', () => {
|
|
37
|
-
colorConversionServiceSpy.hexToRgb.withArgs('#0000FF').and.returnValue([0, 0, 255]); // Blue
|
|
38
|
-
colorConversionServiceSpy.hexToRgb.withArgs('#FF0000').and.returnValue([255, 0, 0]); // Red
|
|
39
|
-
expect(service.darkerColor('#0000FF', '#FF0000')).toBe('#0000FF');
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('should determine lighter color correctly', () => {
|
|
43
|
-
colorConversionServiceSpy.hexToRgb.withArgs('#0000FF').and.returnValue([0, 0, 255]); // Blue
|
|
44
|
-
colorConversionServiceSpy.hexToRgb.withArgs('#FF0000').and.returnValue([255, 0, 0]); // Red
|
|
45
|
-
expect(service.lighterColor('#FF0000', '#0000FF')).toBe('#FF0000');
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('should calculate luminance accurately', () => {
|
|
49
|
-
expect(service.calculateLuminance(255, 255, 255)).toBeCloseTo(255, 2); // White
|
|
50
|
-
expect(service.calculateLuminance(0, 0, 0)).toBeCloseTo(0, 2); // Black
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should fix color to RGB array for hex input', () => {
|
|
54
|
-
colorConversionServiceSpy.hexToRgb.withArgs('#FF0000').and.returnValue([255, 0, 0]);
|
|
55
|
-
expect(service.fixColor('#FF0000')).toEqual([255, 0, 0]);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('should handle 3-digit hex color', () => {
|
|
59
|
-
colorConversionServiceSpy.hexToRgb.withArgs('#F00').and.returnValue([255, 0, 0]);
|
|
60
|
-
expect(service.fixColor('#F00')).toEqual([255, 0, 0]);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('should return empty array for invalid hex color', () => {
|
|
64
|
-
colorConversionServiceSpy.hexToRgb.and.returnValue([]);
|
|
65
|
-
expect(service.fixColor('#GGG')).toEqual([]);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
it('should convert RGB string to array', () => {
|
|
69
|
-
expect(service.fixColor('rgb(100, 200, 50)')).toEqual([100, 200, 50]);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
it('should handle malformed color input', () => {
|
|
73
|
-
expect(service.fixColor('not a color')).toEqual([]);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { Injectable } from '@angular/core'
|
|
2
|
-
import { ColorConversionService } from './color-conversion.service'
|
|
3
|
-
|
|
4
|
-
@Injectable({
|
|
5
|
-
providedIn: 'root'
|
|
6
|
-
})
|
|
7
|
-
export class TextColorService {
|
|
8
|
-
|
|
9
|
-
constructor(
|
|
10
|
-
private colorConversionService: ColorConversionService
|
|
11
|
-
) {}
|
|
12
|
-
|
|
13
|
-
textColorForBgColor(bgColor: string, lightColor: string, darkColor: string) {
|
|
14
|
-
|
|
15
|
-
const UIColors = this.fixColor(bgColor)
|
|
16
|
-
|
|
17
|
-
const r = UIColors[0]
|
|
18
|
-
const g = UIColors[1]
|
|
19
|
-
const b = UIColors[2]
|
|
20
|
-
|
|
21
|
-
return ((r*0.299 + g*0.587 + b*0.114) > 149) ? darkColor : lightColor;
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
darkerColor(color1: string, color2: string) {
|
|
26
|
-
return this.isColorDarker(color1, color2) ? color1 : color2;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
isColorDarker(color1: string, color2: string) {
|
|
30
|
-
|
|
31
|
-
const newColor1 = this.fixColor(color1);
|
|
32
|
-
const newColor2 = this.fixColor(color2);
|
|
33
|
-
|
|
34
|
-
const luminance1 = this.calculateLuminance(newColor1[0], newColor1[1], newColor1[2]);
|
|
35
|
-
const luminance2 = this.calculateLuminance(newColor2[0], newColor2[1], newColor2[2]);
|
|
36
|
-
|
|
37
|
-
return luminance1 < luminance2;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
lighterColor(color1: string, color2: string): string {
|
|
42
|
-
return (this.isColorLighter(color1,color2)) ? color1 : color2;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
isColorLighter(color1: string, color2: string) {
|
|
46
|
-
|
|
47
|
-
const newColor1 = this.fixColor(color1);
|
|
48
|
-
const newColor2 = this.fixColor(color2);
|
|
49
|
-
|
|
50
|
-
const luminance1 = this.calculateLuminance(newColor1[0], newColor1[1], newColor1[2]);
|
|
51
|
-
const luminance2 = this.calculateLuminance(newColor2[0], newColor2[1], newColor2[2]);
|
|
52
|
-
|
|
53
|
-
return (luminance1 > luminance2)
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
calculateLuminance(r: number, g: number, b: number): number {
|
|
58
|
-
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
fixColor(color: string) {
|
|
62
|
-
// Remove leading hash if present
|
|
63
|
-
const sanitizedColor = color.startsWith('#') ? color.slice(1) : color;
|
|
64
|
-
|
|
65
|
-
// Validate if the color is a valid hex (3 or 6 characters)
|
|
66
|
-
if (!this.isValidHex(sanitizedColor)) {
|
|
67
|
-
return this.parseRgb(sanitizedColor); // If not hex, attempt to parse as RGB
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// Convert hex to RGB
|
|
71
|
-
const rgb = this.hexToRgb(sanitizedColor);
|
|
72
|
-
return rgb;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// Helper function to validate if a string is a valid 3 or 6 digit hex code
|
|
76
|
-
isValidHex(color: string): boolean {
|
|
77
|
-
const hexRegex = /^[A-Fa-f0-9]{3}$|^[A-Fa-f0-9]{6}$/i;
|
|
78
|
-
return hexRegex.test(color);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Helper function to convert a 3 or 6 digit hex color to RGB
|
|
82
|
-
hexToRgb(hex: string): number[] {
|
|
83
|
-
if (hex.length === 3) {
|
|
84
|
-
hex = hex.split('').map(c => c + c).join(''); // Expand shorthand hex to full
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return [
|
|
88
|
-
parseInt(hex.slice(0, 2), 16),
|
|
89
|
-
parseInt(hex.slice(2, 4), 16),
|
|
90
|
-
parseInt(hex.slice(4, 6), 16),
|
|
91
|
-
];
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Helper function to parse an RGB string (e.g., rgb(255, 0, 0)) into an array
|
|
95
|
-
parseRgb(rgb: string): number[] {
|
|
96
|
-
const match = rgb.match(/\d+/g);
|
|
97
|
-
return match ? match.map(num => parseInt(num)) : [];
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
package/tsconfig.lib.json
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
|
2
|
-
{
|
|
3
|
-
"extends": "../../tsconfig.json",
|
|
4
|
-
"compilerOptions": {
|
|
5
|
-
"outDir": "../../dist/color-utils",
|
|
6
|
-
"declaration": true,
|
|
7
|
-
"declarationMap": true,
|
|
8
|
-
"inlineSources": true,
|
|
9
|
-
"types": [],
|
|
10
|
-
"baseUrl": "./",
|
|
11
|
-
"paths": {
|
|
12
|
-
"@angular/*": ["node_modules/@angular/*"],
|
|
13
|
-
"color-utils": ["node_modules/color-utils"],
|
|
14
|
-
},
|
|
15
|
-
"target": "ES2022",
|
|
16
|
-
"lib": [
|
|
17
|
-
"dom",
|
|
18
|
-
"ES2022"
|
|
19
|
-
]
|
|
20
|
-
},
|
|
21
|
-
"angularCompilerOptions": {
|
|
22
|
-
"skipTemplateCodegen": true,
|
|
23
|
-
"strictMetadataEmit": true,
|
|
24
|
-
"enableResourceInlining": true,
|
|
25
|
-
"strictInjectionParameters": true,
|
|
26
|
-
"strictTemplates": true
|
|
27
|
-
},
|
|
28
|
-
"exclude": [
|
|
29
|
-
"src/test.ts",
|
|
30
|
-
"**/*.spec.ts"
|
|
31
|
-
]
|
|
32
|
-
}
|
package/tsconfig.lib.prod.json
DELETED
package/tsconfig.spec.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/* To learn more about this file see: https://angular.io/config/tsconfig. */
|
|
2
|
-
{
|
|
3
|
-
"extends": "../../tsconfig.json",
|
|
4
|
-
"compilerOptions": {
|
|
5
|
-
"outDir": "../../out-tsc/spec",
|
|
6
|
-
"types": [
|
|
7
|
-
"jasmine"
|
|
8
|
-
]
|
|
9
|
-
},
|
|
10
|
-
"include": [
|
|
11
|
-
"**/*.spec.ts",
|
|
12
|
-
"**/*.d.ts"
|
|
13
|
-
]
|
|
14
|
-
}
|