-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change getContrastColor -> invertColor while improving implementation…
… and writing new tests
- Loading branch information
1 parent
f275375
commit 984609a
Showing
14 changed files
with
112 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
sidebar_position: 2 | ||
tags: | ||
- validation | ||
--- | ||
|
||
# invertColor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 2 | ||
sidebar_position: 3 | ||
tags: | ||
- validation | ||
--- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 3 | ||
sidebar_position: 4 | ||
tags: | ||
- validation | ||
--- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 4 | ||
sidebar_position: 5 | ||
tags: | ||
- object | ||
--- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 5 | ||
sidebar_position: 6 | ||
tags: | ||
- object | ||
--- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 6 | ||
sidebar_position: 7 | ||
tags: | ||
- array | ||
--- | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './invertColor'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { invertColor } from './invertColor'; | ||
|
||
describe('invertColor()', () => { | ||
describe('when the color is provided as a string', () => { | ||
it('should invert the color to the expected value', () => { | ||
expect(invertColor('#000000')).toBe('#FFFFFF'); | ||
expect(invertColor('#FFFFFF')).toBe('#000000'); | ||
expect(invertColor('#0E0099')).toBe('#F1FF66'); | ||
expect(invertColor('#00FF00')).toBe('#FF00FF'); | ||
expect(invertColor('#FF0100')).toBe('#00FEFF'); | ||
}); | ||
}); | ||
|
||
describe('when the color is provided as an RGB array', () => { | ||
it('should invert the color to the expected value', () => { | ||
expect(invertColor([0, 0, 0])).toBe('#FFFFFF'); | ||
expect(invertColor([255, 255, 255])).toBe('#000000'); | ||
expect(invertColor([121, 52, 38])).toBe('#86CBD9'); | ||
}); | ||
}); | ||
|
||
describe('when the color is provided as a string and the bw option is true', () => { | ||
it('should return black for light colors and white for dark colors', () => { | ||
expect(invertColor('#000000', { bw: true })).toBe('#FFFFFF'); | ||
expect(invertColor('FFFFFF', { bw: true })).toBe('#000000'); | ||
expect(invertColor('#400B06', { bw: true })).toBe('#FFFFFF'); | ||
expect(invertColor('C1EC00', { bw: true })).toBe('#000000'); | ||
expect(invertColor('#005856', { bw: true })).toBe('#FFFFFF'); | ||
}); | ||
}); | ||
|
||
describe('when the color is provided as an RGB array and the bw option is true', () => { | ||
it('should return black for light colors and white for dark colors', () => { | ||
expect(invertColor([0, 0, 0], { bw: true })).toBe('#FFFFFF'); | ||
expect(invertColor([255, 255, 255], { bw: true })).toBe('#000000'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* @name invertColor | ||
* @description - Inverts the color by converting it to its complementary color. | ||
* | ||
* If the color is provided as a string, it should be in hexadecimal format (#RRGGBB). | ||
* If the color is provided as an RGB array, it should be in the format [R, G, B]. | ||
* @param color - The color to invert. | ||
* @param options.bw - If true, returns black (#000000) for light colors and white (#FFFFFF) for dark colors. | ||
* @returns The inverted color in hexadecimal format (#RRGGBB). | ||
*/ | ||
|
||
export type RGB = [red: number, green: number, blue: number]; | ||
|
||
export function invertColor(color: string | RGB, options?: { bw: boolean }) { | ||
let r, g, b; | ||
|
||
if (typeof color === 'string') { | ||
let hex = color; | ||
|
||
if (hex.indexOf('#') === 0) { | ||
hex = color.slice(1); | ||
} | ||
// convert 3-digit hex to 6-digits. | ||
if (hex.length === 3) { | ||
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]; | ||
} | ||
if (hex.length !== 6) { | ||
throw new Error('Invalid HEX color.'); | ||
} | ||
|
||
r = parseInt(hex.substring(0, 2), 16); | ||
g = parseInt(hex.substring(2, 4), 16); | ||
b = parseInt(hex.substring(4, 6), 16); | ||
} else { | ||
r = color[0] as number; | ||
g = color[1] as number; | ||
b = color[2] as number; | ||
} | ||
|
||
if (options?.bw) { | ||
// https://stackoverflow.com/a/3943023/112731 | ||
return r * 0.299 + g * 0.587 + b * 0.114 > 186 ? '#000000' : '#FFFFFF'; | ||
} | ||
|
||
r = (255 - r).toString(16).toUpperCase(); | ||
g = (255 - g).toString(16).toUpperCase(); | ||
b = (255 - b).toString(16).toUpperCase(); | ||
|
||
return '#' + _padZero(r) + _padZero(g) + _padZero(b); | ||
} | ||
|
||
function _padZero(str: string, len: number = 2) { | ||
len = len || 2; | ||
const zeros = new Array(len).join('0'); | ||
return (zeros + str).slice(-len); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters