View/Edit #4342:rgb2ansi

Switch to edit mode.

Name(s): rgb2ansi
1:  "Converts RGB colour tripplets into nearest ANSI colour."
2:  "eg, '9933CC' into {'bold', 'magenta'}"
3:  {rrggbb} = args
4:  rgb = decode_binary(tostr("~", rrggbb[1..2], "~", rrggbb[3..4], "~", rrggbb[5..6]), 1)
5:  "bold = ((rgb[1] + rgb[2]) + rgb[3]) > (127 * 3);"
6:  bold = rgb[1] ^ 2 + rgb[2] ^ 2 + rgb[3] ^ 2 > 127 ^ 2 * 3
7:  "Gives more weighting to a single color: Eg, FF0000 should be bold while 707070 should not. --Cecil"
8:  div = bold ? 127 | 63
9:  $cmd_utils:suspend_if_needed()
10: max = max(@rgb)
11: if (max == 0)
12:   return {"unbold", "black"}
13: else
14:   if (max < div)
15:     rgb[1] = rgb[1] + div - max + 1
16:     rgb[2] = rgb[2] + div - max + 1
17:     rgb[3] = rgb[3] + div - max + 1
18:   endif
19:   return {bold ? "bold" | "unbold", {{{"black", "blue"}, {"green", "cyan"}}, {{"red", "magenta"}, {"yellow", "white"}}}[(rgb[1] > div) + 1][(rgb[2] > div) + 1][(rgb[3] > div) + 1]}
20: endif
21: "Last modified by Dax (#789) on Wed Dec 26 18:09:51 2001 MST."

Verb arguments: this none this
Verb perms: Readable eXecutable NOT-Debug
Verb owned by: Hacker (#60)

You are not logged in.

[home | help | who | search | setup | code]