1// PXPixel.m 2// Pixen 3// 4// Created by Joe Osborn on Thu Sep 11 2003. 5// Copyright (c) 2003 Open Sword Group. All rights reserved. 6// 7 8#import "PXPixel.h" 9 10 11@implementation PXPixel 12 13- (BOOL)isEqual:other 14{ 15 return [other isKindOfClass:[self class]] && [[self color] isEqual:[other color]]; 16} 17 18- (unsigned)hash 19{ 20 return [color hash]; 21} 22 23+ withColor:aColor 24{ 25 return [[[self alloc] initWithColor:aColor] autorelease]; 26} 27 28- initWithColor:aColor 29{ 30 [super init]; 31 [self setColor:aColor]; 32 return self; 33} 34 35- (void)dealloc 36{ 37 [color release]; 38 [super dealloc]; 39} 40 41- color 42{ 43 return color; 44} 45 46- (void)setColor:aColor 47{ 48 id newColor = (([aColor alphaComponent] == 0) ? [NSColor clearColor] : [aColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace]); 49 //newColor = [newColor colorUsingColorSpaceName:NSCalibratedRGBColorSpace] 50 [newColor retain]; 51 [color release]; 52 color = newColor; 53} 54 55- (void)drawAtPoint:(NSPoint)aPoint withOpacity:(float)anOpacity; 56{ 57 // now for a CG implementation! 58 id newColor; 59 if ([color colorSpaceName] != NSCalibratedRGBColorSpace) { newColor = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; } 60 else { newColor = color; } 61 62#ifdef __COCOA__ 63 CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; 64 CGContextSetRGBFillColor(currentContext, [newColor redComponent], [newColor greenComponent], [newColor blueComponent], [newColor alphaComponent]*anOpacity); 65 CGContextFillRect(currentContext, CGRectMake(aPoint.x, aPoint.y, 1, 1)); 66#endif 67 /*NSColor * calibratedColor = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; 68 NSColor * compositeColor; 69 if (anOpacity == 1) 70 { 71 compositeColor = calibratedColor; 72 } 73 else 74 { 75 compositeColor = [NSColor colorWithCalibratedRed:[calibratedColor redComponent] green:[calibratedColor greenComponent] blue:[calibratedColor blueComponent] alpha:[color alphaComponent] * anOpacity]; 76 } 77 [compositeColor set]; 78 NSRectFillUsingOperation(NSMakeRect(aPoint.x, aPoint.y, 1, 1), [compositeColor alphaComponent] == 1 ? NSCompositeCopy : NSCompositeSourceOver); 79 */ 80} 81 82/*we're not actually using this method because there seems to be a weirdness in -[NSMutableDictionary mutableCopy] that makes the mutably copied dictionary worthless-- it doesn't retain its objects! At least, there were crashes when we used that method that didn't show up when we did -(void)setColor:atPoint: for each pixel.*/ 83- copyWithZone:(NSZone *)zone 84{ 85 return [self retain]; 86} 87 88- initWithCoder:coder 89{ 90 [super init]; 91 color = [[coder decodeObjectForKey:@"color"] retain]; 92 return self; 93} 94 95- (void)encodeWithCoder:coder 96{ 97 [coder encodeObject:color forKey:@"color"]; 98} 99 100@end 101