Skip to content

Instantly share code, notes, and snippets.

@st44100
Last active August 29, 2015 14:13
Show Gist options
  • Save st44100/1912065fc7cf82f05712 to your computer and use it in GitHub Desktop.
Save st44100/1912065fc7cf82f05712 to your computer and use it in GitHub Desktop.
CoffeeScript Shade and Tint color without Alpha channel
toRgb = (hex) ->
result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex)
if result
[
parseInt(result[1], 16)
parseInt(result[2], 16)
parseInt(result[3], 16)
]
else
null
mix = (src, target, mix) ->
[
Math.floor(target[0] + (src - target[0]) * mix)
Math.floor(target[1] + (src - target[1]) * mix)
Math.floor(target[2] + (src - target[2]) * mix)
]
shade = (target, m) -> mix 0, target, m
tint = (target, m) -> mix 255, target, m
# Test
rgb = [
255
10
10
]
a = '#FF0101'
console.log(
"#{a} to #{toRgb(a)} =>"
"Shade #{shade(toRgb(a), 0.33)}"
"Tint #{tint(toRgb(a), 0.33)}"
)
# http://hex.colorrrs.com/
@st44100
Copy link
Author

st44100 commented Jan 9, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment