1// 2// PXPalette.m 3// Pixen-XCode 4// 5// Created by Joe Osborn on Wed Jun 09 2004. 6// Copyright (c) 2004 Open Sword Group. All rights reserved. 7// 8 9#import "PXPalette.h" 10 11@implementation PXPalette 12 13- initWithName:aName 14{ 15 return [self initWithName:aName colors:[[NSMutableArray alloc] initWithCapacity:32]]; 16} 17 18- initWithName:aName colors:someColors 19{ 20 [super init]; 21 usedColors = [someColors mutableCopy]; 22 name = [aName copy]; 23 return self; 24} 25 26- (void)setDelegate:anObject 27{ 28 delegate = anObject; 29} 30 31- (void)dealloc 32{ 33 [usedColors release]; 34 [name release]; 35 [super dealloc]; 36} 37 38- (int)addColor:color 39{ 40 if(([color alphaComponent] < .00125) || [usedColors containsObject:color] || [usedColors count] > 255) { return -1; } 41 int index = [usedColors count]; 42 int i; 43 for(i = 0; i < [usedColors count]; i++) 44 { 45 if([[usedColors objectAtIndex:i] alphaComponent] <= .00125) 46 { 47 index = i; 48 break; 49 } 50 } 51 [usedColors insertObject:color atIndex:index]; 52 return index; 53} 54 55- colorAtIndex:(unsigned)index 56{ 57 return [usedColors objectAtIndex:index]; 58} 59 60- (void)setColor:color atIndex:(unsigned)index 61{ 62 while([usedColors count] <= index) 63 { 64 [usedColors addObject:[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:.001]]; 65 } 66 [usedColors replaceObjectAtIndex:index withObject:color]; 67 [self removeDuplicatesOfColorAtIndex:index]; 68} 69 70- (void)removeDuplicatesOfColorAtIndex:(unsigned)index 71{ 72 id color = [usedColors objectAtIndex:index]; 73 int i; 74 for(i = 0; i < [usedColors count]; i++) 75 { 76 id current = [usedColors objectAtIndex:i]; 77 if([current isEqual:color] && (current != color) && ([current alphaComponent] > .00125)) 78 { 79 //Not sure !! (Fabien) 80 [delegate palette:self foundDuplicateColorsAtIndex:index andIndex:i]; 81 } 82 } 83} 84 85- colors 86{ 87 return usedColors; 88} 89 90- (void)setColors:newColors 91{ 92 id old = usedColors; 93 usedColors = [newColors mutableCopy]; 94 [old release]; 95} 96 97- name 98{ 99 return name; 100} 101 102- (void)setName:newName 103{ 104 [name autorelease]; 105 name = [newName copy]; 106} 107 108- copyWithZone:(NSZone *)zone 109{ 110 id copy = [[[self class] allocWithZone:zone] initWithName:name]; 111 [copy setColors:[[usedColors mutableCopy] autorelease]]; 112 return copy; 113} 114 115- initWithCoder:coder 116{ 117 [super init]; 118 name = [[coder decodeObjectForKey:@"name"] retain]; 119 usedColors = [[coder decodeObjectForKey:@"usedColors"] mutableCopy]; 120 return self; 121} 122 123- (void)encodeWithCoder:coder 124{ 125 [coder encodeObject:name forKey:@"name"]; 126 [coder encodeObject:usedColors forKey:@"usedColors"]; 127} 128 129@end