NSValueTransfomers are a very handy solution to store a lots of information in a simple int for example. Combined with bindings one can display text in OS X label colors for example. Something I needed today:
@implementation MGIntToColorTransformer
+ (Class)transformedValueClass {
return [NSColor class];
}
+ (BOOL)allowsReverseTransformation {
return NO;
}
- (id)transformedValue:(id)value {
NSColor *color = nil;
switch ([value intValue]) {
case 0:
color = [NSColor colorWithCalibratedRed:0.298 green:0.298 blue:0.298 alpha:1.000]; // [NSColor blackColor] || [NSColor clearColor]
break;
case 1:
color = [NSColor colorWithCalibratedRed:0.924 green:0.372 blue:0.337 alpha:1.000]; // red
break;
case 2:
color = [NSColor colorWithCalibratedRed:0.907 green:0.628 blue:0.255 alpha:1.000]; // orange
break;
case 3:
color = [NSColor colorWithCalibratedRed:0.845 green:0.771 blue:0.254 alpha:1.000]; // yellow
break;
case 4:
color = [NSColor colorWithCalibratedRed:0.642 green:0.766 blue:0.254 alpha:1.000]; // green
break;
case 5:
color = [NSColor colorWithCalibratedRed:0.335 green:0.599 blue:0.937 alpha:1.000]; // blue
break;
case 6:
color = [NSColor colorWithCalibratedRed:0.712 green:0.522 blue:0.796 alpha:1.000]; // lavender
break;
case 7:
color = [NSColor colorWithCalibratedRed:0.624 green:0.624 blue:0.624 alpha:1.000]; // gray
break;
default:
color = [NSColor colorWithCalibratedRed:0.298 green:0.298 blue:0.298 alpha:1.000];
break;
}
return color;
}
@end
Click here for the new weblog.
Daily Snippet
-
odonnellui781 likes this
-
stephanie620 likes this
-
do-nothing reblogged this from moapp
-
moapp posted this