Last active
April 13, 2020 19:47
-
-
Save EarthenLynx/4578cd5b82091c2d0a795f182cd37c51 to your computer and use it in GitHub Desktop.
Some useful color methods to be used as modular functions. Credit where credit's due: If not my own, the source is given in comments.
This file contains hidden or 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
// Check for color darkness. Returns false of dark'ish, true if bright | |
function checkForBrightness(arr) { | |
let sortedArr = arr.sort; | |
return (sortedArr[0] < 150 && sortedArr[1] < 80 && sortedArr[2] < 80) ? | |
false : true | |
} |
This file contains hidden or 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
// Create a random hex number | |
function createRandomColor() { | |
let rNum = Math.ceil(Math.random() * 16777215).toString(16); | |
return '#'.concat(rNum); | |
} |
This file contains hidden or 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
// Convert rgb arr to rgb string | |
function rgbArrToStr(rgbArr) { | |
let rgbCols = rgbArr.join(','); | |
let rgbStr = 'rgb(' + rgbCols + ')'; | |
return rgbStr; | |
} |
This file contains hidden or 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
// Check if string has rgb format | |
function rgbCheck(color) { | |
// Source for RegExp: https://gist.github.com/sethlopezme/d072b945969a3cc2cc11 | |
let regExpRgb = /^rgb\((0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d),(0|255|25[0-4]|2[0-4]\d|1\d\d|0?\d?\d)\)$/; | |
return regExpRgb.test(color); | |
} |
This file contains hidden or 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
// Convert rgb string to rgb arr | |
function rgbStrToArr(rgbStr) { | |
let rgbCols = rgbStr.slice(4, (rgbStr.length - 1)); | |
let rgbArr = rgbCols.split(','); | |
let rgbArrNum = rgbArr.map(Number); | |
return rgbArrNum; | |
} |
This file contains hidden or 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
function rgbToHex(r, g, b) { | |
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment