PaletteMaker displays a palette with checkmarks showing which colors are currently defined in the ColorPalette. In order to ensure that the checkmark text is visible, PaletteMaker automatically selects either white or black text, based on the "brightness" of the background RGB color. Here's the code that I wrote...
var rgb=bgcolor.substr(1).split(''); var long=bgcolor.length>=6;
function h2d(h){return '0123456789ABCDEF'.indexOf(h?h.toUpperCase():'');};
var r=h2d(rgb[0]); var g=h2d(rgb[long?2:1]); var b=h2d(rgb[long?4:2]);
if (r<0||g<0||b<0||r+g+b>=15) return 'black'; // BAD RGB or BRIGHT COLOR
return 'white'; // DARK COLOR
The background color, t, can be specified as "#RGB", "#RRGGBB", or an X11 Color Name.
Notes:
* first line chops up the color code into separate r, g, b variables.
* if a 6-digit RRGGBB value is used, only the first (most-significant) digit of R, G, and B is used to calculate overall brightness.
* next two lines convert the hexadecimal values (0-F) to decimal (0-15)
* next two lines test for "brightness" and return either black or white, accordingly.
* the "cutoff" value of r+g+b>15 to test "bright" vs. "dark" color was determined empirically by experimentation.
Note that this approach only uses black or white text. I had experimented with calculating the "reciprocal" color, so that, for example, a background of "#ccc" (light gray) would use text color "#333" (dark gray), while a background of "#ccf" (light blue) would use "#ffc" (pale yellow) text color. However, this lead to some poor color combinations, especially when the background color was close to the middle of the RGB palette (e.g., the reciprocal of background "#999" would be text color "#666") due to the low contrast ratio between text and background.
enjoy,
-e
Eric Shulman
TiddlyTools / ELS Design Studios
YOUR DONATIONS ARE VERY IMPORTANT!
HELP ME TO HELP YOU - MAKE A CONTRIBUTION TO MY "TIP JAR"...
Professional TiddlyWiki Consulting Services...
Analysis, Design, and Custom Solutions: