Last active
December 16, 2015 19:59
-
-
Save stuartwakefield/5488961 to your computer and use it in GitHub Desktop.
Some color functions
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
/** | |
* Given an integer representing the hexadecimal | |
* value for a color, returns an array containing | |
* the RGB values from 0 to 255 | |
* @param val the integer representing the color | |
* @returns the array of RGB values | |
*/ | |
function toRGB(val) { | |
var r = (val & 0xFF0000) >>> 16; | |
var g = (val & 0x00FF00) >>> 8; | |
var b = val & 0x0000FF; | |
return [r, g, b]; | |
} | |
/** | |
* Given an array of RGB values from 0 to 255 | |
* this will return the integer value that | |
* represents the color | |
* @param rgb the array of RGB values | |
* @returns the integer representing the color | |
*/ | |
function fromRGB(rgb) { | |
return rgb[0] << 16 | rgb[1] << 8 | rgb[2]; | |
} | |
/** | |
* Gets the luma value based upon ITU-R Rec 709 | |
* coefficients. | |
* @param val the integer color value | |
* @returns the luma value | |
*/ | |
function getPhotometricLuma(val) { | |
var rgb = fromRGB(val); | |
return 0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment