Last active
August 29, 2015 14:13
-
-
Save st44100/1912065fc7cf82f05712 to your computer and use it in GitHub Desktop.
CoffeeScript Shade and Tint color without Alpha channel
This file contains hidden or 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
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/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For testing
http://hex.colorrrs.com/