-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from studio-YOLO/bugfix-higher-contrast-function
Fixed bug where the function used the old color scheme
- Loading branch information
Showing
1 changed file
with
12 additions
and
6 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 |
---|---|---|
@@ -1,13 +1,19 @@ | ||
/** | ||
* Function that given a color in the format [R,G,B] converts it to grayscale in the format [R,G,B] | ||
* Function that given a color in the RGB format converts it to grayscale | ||
* and then calculates the color (black or white) with the highest contrast. | ||
* @param {number[]} color Color that has to be converted and compared in the format [R,G,B] | ||
* @returns {number[]} Color with the higher contrast of the input color in the format [R,G,B] | ||
* @param {number} r red color value | ||
* @param {number} g green color value | ||
* @param {number} b blue color value | ||
* @returns {object} Color with the higher contrast of the input color in the format {r: value, g:value, b:value} | ||
*/ | ||
function higherColorContrast(color) { | ||
const gray = Math.round(0.299 * color[0] + 0.587 * color[1] + 0.114 * color[2]) | ||
function higherColorContrast(r, g, b) { | ||
const gray = Math.round(0.299 * r + 0.587 * g + 0.114 * b) | ||
const contrastColor = gray >= 128 ? 0 : 255; | ||
return [contrastColor, contrastColor, contrastColor]; | ||
return { | ||
r: contrastColor, | ||
g: contrastColor, | ||
b: contrastColor | ||
}; | ||
} | ||
|
||
export default higherColorContrast; |