This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: Colorful Extensions
Feed Title: Travis Griggs - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/travis-rss.xml
Feed Description: This TAG Line is Extra
I do lots of custom widgets. Smalltalk is just fun to make new and interesting ways to display data in. So one of the things I've ended up extending is ColorValue. These aren't as novel, but they've been incredibly handy for doing colorful widgets.
cornerContrast
Make a colored region, and then print some text on it. What color is the text? Especially if you're not sure what color to make the region and you're still experimenting, or even more serious your widget colors itself dynamically based on some external resource. This method:
works off a receiver color (e.g. your colored region) and returns the color which is one of the RGB color cubes corners, the corner that's furthest from the receiver. Try it, it's kind of fun.
bestContrast
The above said, most viewers cotton to black or white text better then color contrasted text, so nowdays, I tend to use this method:
It just uses the luminosity of the receiver (an interesting equation for the interested student) and goes either black or white. Both of these methods nicely couple two colors together where one is intended to be a contrast of the other.
But why not 8 bits?
One thing that always fascinates me when I'm mucking around in the ColorValue objects, is why they use scaled RGB values of 8096 (13 bits). What's the point really? Is it in case someone is doing really high end high precision graphics beyond 8 bits? In Smalltalk? Let's get real. Many years ago, before the world pretty much settled that 8 bits per channel was enough for 99.9999% of all applications, it might have made sense. Changing them to 8 bits would dramatically simplify/expedite the interchange between Smalltalk ColorValues and those that come from the platform. And if someone really needs the high precision version... they can always subclass. And in that event, go with 16 bits per channel. I just can't figure out what was so lucky about 13.