1//
2//  PXGradientBuilderController.m
3//  Pixen-XCode
4//
5//  Created by Ian Henderson on 26.08.04.
6//  Copyright 2004 Open Sword Group. All rights reserved.
7//
8
9#import "PXGradientBuilderController.h"
10#import "PXPaletteSwitcher.h"
11#import "PXPalette.h"
12
13@implementation PXGradientBuilderController
14
15- init
16{
17	return [super initWithWindowNibName:@"PXGradientBuilder"];
18}
19
20- initWithPaletteSwitcher:(PXPaletteSwitcher *)aSwitcher
21{
22	[self init];
23	switcher = aSwitcher;
24	return self;
25}
26
27- (void)beginSheetInWindow:window
28{
29    [NSApp beginSheet:[self window] modalForWindow:window modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
30}
31
32- (IBAction)create:sender
33{
34	NSMutableArray *colors = [NSMutableArray arrayWithCapacity:[colorsField intValue]];
35	int i;
36	float r, g, b, a, deltaR, deltaG, deltaB, deltaA;
37	int colorCount = [colorsField intValue];
38	NSColor *startColor = [[startColorWell color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
39	NSColor *endColor = [[endColorWell color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
40	r = [startColor redComponent];
41	g = [startColor greenComponent];
42	b = [startColor blueComponent];
43	a = [startColor alphaComponent];
44	deltaR = ([endColor redComponent] - r) / colorCount;
45	deltaG = ([endColor greenComponent] - g) / colorCount;
46	deltaB = ([endColor blueComponent] - b) / colorCount;
47	deltaA = ([endColor alphaComponent] - a) / colorCount;
48
49	for (i=0; i<colorCount; i++) {
50		[colors addObject:[NSColor colorWithCalibratedRed:r green:g blue:b alpha:a]];
51		r += deltaR;
52		g += deltaG;
53		b += deltaB;
54		a += deltaA;
55	}
56
57	PXPalette *palette = [[[PXPalette alloc] initWithName:[nameField stringValue] colors:colors] autorelease];
58
59	[switcher addNewPalette:palette withName:[nameField stringValue] replacingPaletteAtIndex:[switcher indexOfPalette:palette]];
60    [NSApp endSheet:[self window]];
61    [self close];
62}
63
64- (IBAction)cancel:sender
65{
66    [NSApp endSheet:[self window]];
67    [self close];
68}
69
70@end
71