I'm currently using the following code to output text in green to the terminal:
printf("%c[1;32mHello, world!\n", 27);
However, I want more shades of green. What's the easiest way to accomplish this?
You can use the xterm sample 256colors2.pl script described on Rob Meerman's site to make sure that your terminal handles 256 colors correctly. Then choose the right combination of RGB values to give you the right shade of green.
Based on that script, it looks like the color numbers are essentially an offset of a base 6 color scheme:
COLOR = r*6^2 + g*6 + b) + 16
And for the foreground color we need to use:
\x1b[38;5;${COLORNUM}m
And again based on the script, here's a (perl) loop that displays the letter O in the desired color:
# now the color values
for ($green = 0; $green < 6; $green++) {
for ($red = 0; $red < 6; $red++) {
for ($blue = 0; $blue < 6; $blue++) {
$color = 16 + ($red * 36) + ($green * 6) + $blue;
print "\\x1b[38;5;${color}m :\x1b[38;5;${color}m O\x1b[0m ";
print "\n" if ($blue == 2 || $blue == 5);
}
}
print "\n";
}
And here's a sample of its output:
NOTE: Charles seems to quite a bit more about how it actually works and what you'll need to do to verify that the the shell supports the required capabilities. My information is based strictly on observation and testing with a shell known to support 256 colors (konsole).