Skip to content

Commit

Permalink
Merge pull request #53 from studio-YOLO/bugfix-higher-contrast-function
Browse files Browse the repository at this point in the history
Fixed bug where the function used the old color scheme
  • Loading branch information
VinciGit00 authored May 4, 2024
2 parents 7dbbce1 + 32aa4f2 commit bf94b72
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/core/higher-contrast.js
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;

0 comments on commit bf94b72

Please sign in to comment.