Skip to content

Commit 136bb76

Browse files
committed
add threshold, default export
1 parent 1fc2bfb commit 136bb76

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/invert.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ export type Color = RGB | RgbArray | HexColor;
2929
export interface BlackWhite {
3030
black: HexColor;
3131
white: HexColor;
32+
threshold?: number
3233
}
3334

3435
// -------------------------------
3536
// CONSTANTS
3637
// -------------------------------
3738

38-
const BW_THRESHOLD = Math.sqrt(1.05 * 0.05) - 0.05;
39+
const DEFAULT_THRESHOLD = Math.sqrt(1.05 * 0.05) - 0.05;
3940
const RE_HEX = /^(?:[0-9a-f]{3}){1,2}$/i;
4041
const DEFAULT_BW: BlackWhite = {
4142
black: '#000000',
42-
white: '#ffffff'
43+
white: '#ffffff',
44+
threshold: DEFAULT_THRESHOLD
4345
};
4446

4547
// -------------------------------
@@ -86,12 +88,12 @@ function getLuminance(c: RgbArray): number {
8688
}
8789

8890
function invertToBW(color, bw: BlackWhite | boolean, asArr?: boolean): RgbArray | HexColor {
89-
const colors = (bw === true)
91+
const options = (bw === true)
9092
? DEFAULT_BW
9193
: 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);
9597
}
9698

9799
// -------------------------------
@@ -121,11 +123,11 @@ namespace invert {
121123
* Generates inverted (opposite) version of the given color, as a RGB object.
122124
* @alias invert.asRgbObject
123125
* @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
125127
* black or white. Provide an object to customize black/white colors.
126128
* @returns {RGB} - RGB object representation of the inverted color.
127129
*/
128-
export function asRGB(color: Color, bw: BlackWhite | boolean = false): RGB {
130+
export function asRGB(color: Color, bw?: BlackWhite | boolean): RGB {
129131
color = toRgbArray(color);
130132
const list: RgbArray = bw
131133
? invertToBW(color, bw, true) as RgbArray
@@ -136,17 +138,19 @@ namespace invert {
136138
/**
137139
* Generates inverted (opposite) version of the given color, as a RGB array.
138140
* @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
140142
* black or white. Provide an object to customize black/white colors.
141143
* @returns {RGB} - RGB array representation of the inverted color.
142144
*/
143-
export function asRgbArray(color: Color, bw: BlackWhite | boolean = false): RgbArray {
145+
export function asRgbArray(color: Color, bw?: BlackWhite | boolean): RgbArray {
144146
color = toRgbArray(color);
145147
return bw
146148
? invertToBW(color, bw, true) as RgbArray
147149
: color.map(c => 255 - c) as RgbArray;
148150
}
149151

152+
export const defaultThreshold = DEFAULT_THRESHOLD;
153+
150154
/**
151155
* Alias of `.asRGB()`
152156
*/
@@ -157,6 +161,4 @@ namespace invert {
157161
// EXPORT
158162
// -------------------------------
159163

160-
// dual export
161164
export default invert;
162-
export { invert };

0 commit comments

Comments
 (0)