-
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 #22 from studio-YOLO/21-higher-contrast
Implemented test and demo script for higher contrast function
- Loading branch information
Showing
4 changed files
with
60 additions
and
15 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,23 @@ | ||
import EditPix from "../../src/editpix.js"; | ||
|
||
const editpix = new EditPix(); | ||
|
||
//color | ||
const hexColor = "#78828c"; | ||
|
||
//convert color from hex to rgb | ||
const rgbColor = editpix.convertToRgb(hexColor); | ||
|
||
//get the higher contrast color | ||
const higherColorContrastRgb = editpix.getHigherContrast(rgbColor); | ||
|
||
//convert higher contrast color from rgb to hex | ||
const higherColorContrastHex = editpix.convertToHex(higherColorContrastRgb); | ||
|
||
|
||
//display results | ||
document.body.style.backgroundColor = hexColor; | ||
const contrastText = document.createElement("h1"); | ||
contrastText.textContent = "Hello World!" | ||
contrastText.style.color = higherColorContrastHex; | ||
document.body.appendChild(contrastText) |
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,21 +1,13 @@ | ||
/** | ||
* Function that given a color in the format [R,G,B] converts it to grayscale in the format [R,G,B] | ||
* and then calculates the color with the highest contrast. | ||
* 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] | ||
*/ | ||
function higherColorContrast(color) { | ||
// Convert the color to grayscale | ||
const grayscale = [ | ||
Math.round(0.299 * color[0] + 0.587 * color[1] + 0.114 * color[2]), | ||
Math.round(0.299 * color[0] + 0.587 * color[1] + 0.114 * color[2]), | ||
Math.round(0.299 * color[0] + 0.587 * color[1] + 0.114 * color[2]) | ||
]; | ||
|
||
// Calculate the color with the highest contrast | ||
const contrastColor = grayscale.map(channel => channel > 128 ? 0 : 255); | ||
|
||
return contrastColor; | ||
const gray = Math.round(0.299 * color[0] + 0.587 * color[1] + 0.114 * color[2]) | ||
const contrastColor = gray >= 128 ? 0 : 255; | ||
return [contrastColor, contrastColor, contrastColor]; | ||
} | ||
|
||
export default higherColorContrast; |
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