Skip to content

Instantly share code, notes, and snippets.

@nikoloza
Last active February 14, 2017 11:09
Show Gist options
  • Save nikoloza/dd85f34dd71125db722d791d68ae154c to your computer and use it in GitHub Desktop.
Save nikoloza/dd85f34dd71125db722d791d68ae154c to your computer and use it in GitHub Desktop.
Combine RGB(A) colors with specific range
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)
)
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