Last active
February 14, 2017 11:09
-
-
Save nikoloza/dd85f34dd71125db722d791d68ae154c to your computer and use it in GitHub Desktop.
Combine RGB(A) colors with specific range
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
function mixTwoRgb (colors, range = 50) { | |
let arr = [], i = 0 | |
for (let i = 0; i < 3; i++) { | |
arr[i] = Math.round( | |
colors[0][i] + ( | |
(colors[1][i] - colors[0][i]) * range / 100 | |
) | |
) | |
} | |
return `rgb(${arr})` | |
} | |
console.log( | |
mixTwoRgb([[234, 126, 53], [115, 172, 77]], 30) | |
) |
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
function mixTwoRgba (colors, range = 50) { | |
let arr = [], i = 0 | |
for (let i = 0; i < 4; i++) { | |
let round = (i == 3) ? (x) => x : Math.round | |
arr[i] = round( | |
(colors[0][i] + ( | |
(colors[1][i] - colors[0][i]) * range / 100 | |
)) | |
) | |
} | |
return `rgba(${arr})` | |
} | |
console.log( | |
mixTwoRgba([[234, 126, 53, 0.5], [115, 172, 77, 0.2]], 75) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment