Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/// color_find(str)
// Accepts color names, "#RGB", "#RRGGBB".
var i, r, g, b, s = string_lower(argument0), hex = "012345679abcdef";
if (string_char_at(s, 1) == "#") {
i = string_length(s) - 1
s = string_copy(s, 2, i)
if (i == 3) { // #RGB
r = string_pos(string_char_at(s, 1), hex) - 1
g = string_pos(string_char_at(s, 2), hex) - 1
b = string_pos(string_char_at(s, 3), hex) - 1
if (r < 0 || g < 0 || b < 0) return -1
return r | (r << 4) | (g << 8) | (g << 12) | (b << 16) | (b << 20)
} else if (i == 6) { // #RRGGBB
r = string_pos(string_char_at(s, 1), hex) - 1
g = string_pos(string_char_at(s, 3), hex) - 1
b = string_pos(string_char_at(s, 5), hex) - 1
if (r < 0 || g < 0 || b < 0) return -1
i = (r << 4) | (g << 12) | (b << 20)
r = string_pos(string_char_at(s, 2), hex) - 1
g = string_pos(string_char_at(s, 4), hex) - 1
b = string_pos(string_char_at(s, 6), hex) - 1
if (r < 0 || g < 0 || b < 0) return -1
return i | r | (g << 8) | (b << 16)
}
}
switch (s) {
case "white": return $fcfcfc
case "black": return color_find("#334")
case "gray": case "grey": return color_find("#999")
case "red": return $6674FA
case "green": return color_find("#7e4")
case "blue": return $FABA66
//
case "aqua": return color_find("#44e7f0")
case "yellow": return $55ccff
case "pink": return color_find("#f7e")
//
case "orange": return color_find("#fa5")
case "lime": return color_find("#df4")
case "purple": return color_find("#a6f")
case "night": return color_find("#458")
case "fab": return color_find("#f0c")
default: return -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.