So, I have already seen a few people not entirely happy with the way I implementing coloring of groups in the newest Reggy release. I figured I would just give my view of what I did and why.
My goal with Reggy from the beginning was to be simple and not look like junk. There are many other regular expression testers out there. If you like them, use them. I happen to think many of them look like dirt. Reggy may not be gorgeous, but it’s certainly not dirt either.
The first implementation of coloring was actually by a volunteer (Justin Bakse) who was nice enough to pass the code back to me to look over. It was a good start for sure. But it used an implementation that I just don’t want — for simplicity’s sake: you pick X amount of colors, then for each match the code picks one of the colors, then the next and so on, repeating. This is visually distinctive if you pick a bunch of crazy colors, but I am still thinking that picking 10 colors in the preferences is not the Mac experience I want to provide for people.
My implementation looks like this:
// Add new color to matched ranges
bool countUp = YES;
float r,g,b,a;
[[NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@”match_color”]] getRed:&r green:&g blue:&b alpha:&a];
for ( unsigned int i = 0; i < [matchedRanges count]; i++ )
{
NSRange thisRange = NSRangeFromString([matchedRanges objectAtIndex:i]);
// Generate new color from base color
if ( i > 0 && [[NSUserDefaults standardUserDefaults] boolForKey:@”color_submatches”] )
{
if ( r > 0.8 || g > 0.8 || b > 0.8 ) countUp = NO;
if ( r < 0.0 || g < 0.0 || b < 0.0 ) countUp = YES;
if ( countUp ) { r += 0.2; g += 0.2; b += 0.2; } else { r -= 0.2; g -= 0.2; b -= 0.2; }
}
[[testingStringField textStorage] addAttribute:NSForegroundColorAttributeName value:[NSColor colorWithCalibratedRed:r green:g blue:b alpha:a] range:thisRange];
}
Pretty simple I think. Get the color components of the match string color preference (r, g, b, a). Then for every match other than the first match, increment each RGB component by 20% (this is a 0 to 1 scale) or decrement depending on the component values. I came out with 0.8 as a maximum value by just testing a bunch of different match colors and found 0.8 to be around the acceptable range of readability against a white background for most colors. The increment or decrement step value of 0.2 was another value I just played around with until there was a good balance between having each color step look different, yet still provide a small enough value so as to allow as many steps as possible between 0 and 1.
If anyone has a more elegant solution, please propose it. I am not a genius, I just know what I like. And I don’t like having to pick a bunch of colors. I like the simplicity of using one color with differing brightness as it still seems that the user has control over the coloring, but maybe I could go for a color generation scheme that is based on color theories like complements or triads. Or maybe I am the only one that is this picky?
Tags: code, opensource, reggy Add Comment »