Last active
August 29, 2015 14:17
-
-
Save InfamousVague/f54ab0c333b679d0b618 to your computer and use it in GitHub Desktop.
Percentage to RGB gradient
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
const rgbp = (r,g,b) => { // takes rgb values as percents | |
// if its a percent, get the percentage of the max color range, else just use | |
// what we are given. | |
// Schema: | |
// color[Int <percent/raw>, String('p' <percent>, 'r' <raw>), Int <min>, Int <max>] | |
let getRangePercent = (percent, min, max) => Math.floor(((percent / 100) * (max-min)) + min); | |
let r = (r[1] === 'p') ? getRangePercent(r[0], r[2], r[3]) : r[0], | |
g = (g[1] === 'p') ? getRangePercent(g[0], g[2], g[3]) : g[0], | |
b = (b[1] === 'p') ? getRangePercent(b[0], b[2], b[3]) : b[0]; | |
r = (r > 255) ? 255 : r; | |
g = (g > 255) ? 255 : g; | |
b = (b > 255) ? 255 : b; | |
return `rgb(${r}, ${g}, ${b})`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment