@@ -5,15 +5,11 @@ type RgbType = {
5
5
} ;
6
6
7
7
export class Colors {
8
- result : string [ ] ;
9
- limit : number ;
10
- prefix : boolean ;
8
+ result : string [ ] = [ ] ;
9
+ limit : number = 0 ;
10
+ prefix : boolean = false ;
11
11
12
- constructor ( ) {
13
- this . result = [ ] ;
14
- this . limit = 0 ;
15
- this . prefix = false ;
16
- }
12
+ constructor ( ) { }
17
13
18
14
#hex2Rgb( hex : string ) {
19
15
const sections = this . #hex2Groups( hex ) ;
@@ -51,23 +47,28 @@ export class Colors {
51
47
}
52
48
53
49
#padHex( color : string ) {
54
- return `${ color [ 0 ] . padStart ( 2 , color [ 0 ] ) } ${ color [ 1 ] . padStart ( 2 , color [ 1 ] ) } ${ color [ 2 ] . padStart ( 2 , color [ 2 ] ) } ` ;
50
+ return `${ color . padStart ( 6 , color [ 0 ] ) } `
55
51
}
56
52
57
- #generateShades( rgb : RgbType , percentage : number ) {
58
- const rgbShade = {
59
- red : this . #getShade( rgb . red , this . limit ) ,
60
- green : this . #getShade( rgb . green , this . limit ) ,
61
- blue : this . #getShade( rgb . blue , this . limit ) ,
62
- } ;
63
- const hex = this . #rgb2hex( rgbShade ) ;
64
- this . result . push ( `${ this . prefix ? '#' + hex : hex } ` ) ;
65
- this . limit += percentage ;
66
- if ( this . limit >= 100 ) {
67
- return ;
53
+ #generate( rgb : RgbType , percentage : number , type : 'shades' | 'tints' ) {
54
+ const color : RgbType = { red : 0 , green : 0 , blue : 0 } ;
55
+
56
+ if ( type === 'shades' ) {
57
+ color . red = this . #getShade( rgb . red , this . limit ) ;
58
+ color . green = this . #getShade( rgb . green , this . limit ) ;
59
+ color . blue = this . #getShade( rgb . blue , this . limit ) ;
60
+ } else {
61
+ color . red = this . #getTint( rgb . red , this . limit ) ;
62
+ color . green = this . #getTint( rgb . green , this . limit ) ;
63
+ color . blue = this . #getTint( rgb . blue , this . limit ) ;
64
+ }
65
+
66
+ while ( this . limit < 100 ) {
67
+ const hex = this . #rgb2hex( color ) ;
68
+ this . result . push ( `${ this . prefix ? '#' + hex : hex } ` ) ;
69
+ this . limit += percentage ;
68
70
}
69
71
70
- this . #generateShades( rgb , percentage ) ;
71
72
}
72
73
73
74
#getTint( color : number , percentage : number ) {
@@ -76,21 +77,6 @@ export class Colors {
76
77
return ( tint > 255 ) ? 255 : tint ;
77
78
}
78
79
79
- #generateTints( rgb : RgbType , percentage : number ) {
80
- this . limit += percentage ;
81
- const rgbTint = {
82
- red : this . #getTint( rgb . red , this . limit ) ,
83
- green : this . #getTint( rgb . green , this . limit ) ,
84
- blue : this . #getTint( rgb . blue , this . limit ) ,
85
- } ;
86
- const hex = this . #rgb2hex( rgbTint ) ;
87
- this . result . push ( `${ this . prefix ? '#' + hex : hex } ` ) ;
88
- if ( this . limit >= 100 ) {
89
- return ;
90
- }
91
- this . #generateTints( rgb , percentage ) ;
92
- }
93
-
94
80
createColors ( color : string , percentage : number , prefix : boolean = false ) {
95
81
// Check if the input exists
96
82
if ( ! color || ! percentage ) {
@@ -100,14 +86,10 @@ export class Colors {
100
86
if ( typeof color !== 'string' || typeof percentage !== 'number' || typeof prefix !== 'boolean' ) {
101
87
throw new Error ( 'Invalid input. Wrong input types are given.' ) ;
102
88
}
103
- // Check if the percentage is valid
104
- if ( percentage < 0 || percentage > 100 ) {
105
- throw new Error ( 'Invalid input. Invalid percentage value is given.' )
106
- }
107
- // Check if HEX is valid
108
- if ( color . length !== 3 && color . length !== 6 ) {
109
- throw new Error ( "Invalid input. Not a valid length." ) ;
89
+ if ( color . length !== 3 && color . length !== 6 || percentage < 0 || percentage > 100 ) {
90
+ throw new Error ( 'Invalid input. Wrong input values are given.' ) ;
110
91
}
92
+
111
93
// Pad the HEX string if it has only 3 characters
112
94
if ( color . length === 3 ) {
113
95
color = this . #padHex( color ) ;
@@ -118,15 +100,16 @@ export class Colors {
118
100
119
101
const rgb = this . #hex2Rgb( color ) ;
120
102
// Generate Tints
121
- this . #generateTints ( rgb , percentage ) ;
103
+ this . #generate ( rgb , percentage , 'tints' ) ;
122
104
// Reset Limit
123
105
this . limit = 0 ;
124
106
// Generate Shades
125
- this . #generateShades ( rgb , percentage ) ;
107
+ this . #generate ( rgb , percentage , 'shades' ) ;
126
108
127
109
// Arrange colors in ascending order
128
- const tints = this . result . slice ( 0 , this . result . length / 2 ) . reverse ( ) ;
129
- const shades = this . result . slice ( this . result . length / 2 , this . result . length ) . reverse ( ) ;
110
+ const mid = this . result . length / 2
111
+ const tints = this . result . slice ( 0 , mid ) . reverse ( ) ;
112
+ const shades = this . result . slice ( mid ) . reverse ( ) ;
130
113
shades . unshift ( `${ this . prefix ? '#' + color : color } ` ) ;
131
114
132
115
// Reset global variables
0 commit comments