@@ -29,17 +29,19 @@ export type Color = RGB | RgbArray | HexColor;
29
29
export interface BlackWhite {
30
30
black : HexColor ;
31
31
white : HexColor ;
32
+ threshold ?: number
32
33
}
33
34
34
35
// -------------------------------
35
36
// CONSTANTS
36
37
// -------------------------------
37
38
38
- const BW_THRESHOLD = Math . sqrt ( 1.05 * 0.05 ) - 0.05 ;
39
+ const DEFAULT_THRESHOLD = Math . sqrt ( 1.05 * 0.05 ) - 0.05 ;
39
40
const RE_HEX = / ^ (?: [ 0 - 9 a - f ] { 3 } ) { 1 , 2 } $ / i;
40
41
const DEFAULT_BW : BlackWhite = {
41
42
black : '#000000' ,
42
- white : '#ffffff'
43
+ white : '#ffffff' ,
44
+ threshold : DEFAULT_THRESHOLD
43
45
} ;
44
46
45
47
// -------------------------------
@@ -86,12 +88,12 @@ function getLuminance(c: RgbArray): number {
86
88
}
87
89
88
90
function invertToBW ( color , bw : BlackWhite | boolean , asArr ?: boolean ) : RgbArray | HexColor {
89
- const colors = ( bw === true )
91
+ const options = ( bw === true )
90
92
? DEFAULT_BW
91
93
: Object . assign ( { } , DEFAULT_BW , bw ) ;
92
- return getLuminance ( color ) > BW_THRESHOLD
93
- ? ( asArr ? hexToRgbArray ( colors . black ) : colors . black )
94
- : ( asArr ? hexToRgbArray ( colors . white ) : colors . white ) ;
94
+ return getLuminance ( color ) > options . threshold
95
+ ? ( asArr ? hexToRgbArray ( options . black ) : options . black )
96
+ : ( asArr ? hexToRgbArray ( options . white ) : options . white ) ;
95
97
}
96
98
97
99
// -------------------------------
@@ -121,11 +123,11 @@ namespace invert {
121
123
* Generates inverted (opposite) version of the given color, as a RGB object.
122
124
* @alias invert.asRgbObject
123
125
* @param {Color } color - Color to be inverted.
124
- * @param {BlackWhite|boolean } [bw=false ] - Whether to amplify the inversion to
126
+ * @param {BlackWhite|boolean } [bw] - Whether to amplify the inversion to
125
127
* black or white. Provide an object to customize black/white colors.
126
128
* @returns {RGB } - RGB object representation of the inverted color.
127
129
*/
128
- export function asRGB ( color : Color , bw : BlackWhite | boolean = false ) : RGB {
130
+ export function asRGB ( color : Color , bw ? : BlackWhite | boolean ) : RGB {
129
131
color = toRgbArray ( color ) ;
130
132
const list : RgbArray = bw
131
133
? invertToBW ( color , bw , true ) as RgbArray
@@ -136,17 +138,19 @@ namespace invert {
136
138
/**
137
139
* Generates inverted (opposite) version of the given color, as a RGB array.
138
140
* @param {Color } color - Color to be inverted.
139
- * @param {BlackWhite|boolean } [bw=false ] - Whether to amplify the inversion to
141
+ * @param {BlackWhite|boolean } [bw] - Whether to amplify the inversion to
140
142
* black or white. Provide an object to customize black/white colors.
141
143
* @returns {RGB } - RGB array representation of the inverted color.
142
144
*/
143
- export function asRgbArray ( color : Color , bw : BlackWhite | boolean = false ) : RgbArray {
145
+ export function asRgbArray ( color : Color , bw ? : BlackWhite | boolean ) : RgbArray {
144
146
color = toRgbArray ( color ) ;
145
147
return bw
146
148
? invertToBW ( color , bw , true ) as RgbArray
147
149
: color . map ( c => 255 - c ) as RgbArray ;
148
150
}
149
151
152
+ export const defaultThreshold = DEFAULT_THRESHOLD ;
153
+
150
154
/**
151
155
* Alias of `.asRGB()`
152
156
*/
@@ -157,6 +161,4 @@ namespace invert {
157
161
// EXPORT
158
162
// -------------------------------
159
163
160
- // dual export
161
164
export default invert ;
162
- export { invert } ;
0 commit comments