forked from flit/MidiKeys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTGradient.m
773 lines (603 loc) · 20.3 KB
/
CTGradient.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
//
// CTGradient.m
//
// Created by Chad Weider on 12/3/05.
// Copyright (c) 2005 Cotingent.
// Some rights reserved: <http://creativecommons.org/licenses/by/2.5/>
//
#import "CTGradient.h"
@interface CTGradient (Private)
- (void)_commonInit;
- (void)addElement:(CTGradientElement*)newElement;
- (CTGradientElement *)elementAtIndex:(unsigned)index;
- (CTGradientElement)removeElementAtIndex:(unsigned)index;
- (CTGradientElement)removeElementAtPosition:(float)position;
@end
@implementation CTGradient
/////////////////////////////////////Initialization Type Stuff
void linearEvaluation (void *info, const float *in, float *out);
static const CGFunctionCallbacks _CTLinearGradientFunction = { 0, &linearEvaluation, NULL }; //Version, evaluator function, cleanup function
- (id)init
{
self = [super init];
if (self != nil)
{
[self _commonInit];
}
return self;
}
- (void)_commonInit
{
elementList = nil;
static const float input_value_range [2] = { 0, 1 }; //range for the evaluator input
static const float output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 }; //ranges for the evaluator output (4 returned values)
gradientFunction = CGFunctionCreate(&elementList, //the two transition colors
1, input_value_range, //number of inputs (just fraction of progression)
4, output_value_ranges, //number of outputs RGBa
&_CTLinearGradientFunction); //info for using the evaluator funtion
}
- (void)dealloc
{
CGFunctionRelease(gradientFunction);
CTGradientElement *elementToRemove = elementList;
while(elementList != nil)
{
elementToRemove = elementList;
elementList = elementList->nextElement;
free(elementToRemove);
}
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone
{
CTGradient *copy = [[[self class] allocWithZone:zone] init];
//now just copy my elementlist
CTGradientElement *currentElement = elementList;
while(currentElement != nil)
{
[copy addElement:currentElement];
currentElement = currentElement->nextElement;
}
return copy;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
if([coder allowsKeyedCoding])
{
unsigned count = 0;
CTGradientElement *currentElement = elementList;
while(currentElement != nil)
{
[coder encodeValueOfObjCType:@encode(float) at:&(currentElement->red)];
[coder encodeValueOfObjCType:@encode(float) at:&(currentElement->green)];
[coder encodeValueOfObjCType:@encode(float) at:&(currentElement->blue)];
[coder encodeValueOfObjCType:@encode(float) at:&(currentElement->alpha)];
[coder encodeValueOfObjCType:@encode(float) at:&(currentElement->position)];
count++;
currentElement = currentElement->nextElement;
}
[coder encodeInt:count forKey:@"CTGradientElementCount"];
}
else
[NSException raise:NSInvalidArchiveOperationException format:@"Only supports NSKeyedArchiver coders"];
}
- (id)initWithCoder:(NSCoder *)coder
{
[self _commonInit];
unsigned count = [coder decodeIntForKey:@"CTGradientElementCount"];
while(count != 0)
{
CTGradientElement newElement;
[coder decodeValueOfObjCType:@encode(float) at:&(newElement.red)];
[coder decodeValueOfObjCType:@encode(float) at:&(newElement.green)];
[coder decodeValueOfObjCType:@encode(float) at:&(newElement.blue)];
[coder decodeValueOfObjCType:@encode(float) at:&(newElement.alpha)];
[coder decodeValueOfObjCType:@encode(float) at:&(newElement.position)];
count--;
[self addElement:&newElement];
}
return self;
}
#pragma mark -
#pragma mark Creation
+ (id)gradientWithBeginningColor:(NSColor *)begin endingColor:(NSColor *)end
{
id newInstance = [[[self class] alloc] init];
CTGradientElement color1;
CTGradientElement color2;
[[begin colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&color1.red
green:&color1.green
blue:&color1.blue
alpha:&color1.alpha];
[[end colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&color2.red
green:&color2.green
blue:&color2.blue
alpha:&color2.alpha];
color1.position = 0;
color2.position = 1;
[newInstance addElement:&color1];
[newInstance addElement:&color2];
return [newInstance autorelease];
}
+ (id)aquaSelectedGradient
{
id newInstance = [[[self class] alloc] init];
CTGradientElement color1;
color1.red = 0.58;
color1.green = 0.86;
color1.blue = 0.98;
color1.alpha = 1.00;
color1.position = 0;
CTGradientElement color2;
color2.red = 0.42;
color2.green = 0.68;
color2.blue = 0.90;
color2.alpha = 1.00;
color2.position = 11.5/23;
CTGradientElement color3;
color3.red = 0.64;
color3.green = 0.80;
color3.blue = 0.94;
color3.alpha = 1.00;
color3.position = 11.5/23;
CTGradientElement color4;
color4.red = 0.56;
color4.green = 0.70;
color4.blue = 0.90;
color4.alpha = 1.00;
color4.position = 1;
[newInstance addElement:&color1];
[newInstance addElement:&color2];
[newInstance addElement:&color3];
[newInstance addElement:&color4];
return [newInstance autorelease];
}
+ (id)aquaNormalGradient
{
id newInstance = [[[self class] alloc] init];
CTGradientElement color1;
color1.red = color1.green = color1.blue = 0.95;
color1.alpha = 1.00;
color1.position = 0;
CTGradientElement color2;
color2.red = color2.green = color2.blue = 0.83;
color2.alpha = 1.00;
color2.position = 11.5/23;
CTGradientElement color3;
color3.red = color3.green = color3.blue = 0.95;
color3.alpha = 1.00;
color3.position = 11.5/23;
CTGradientElement color4;
color4.red = color4.green = color4.blue = 0.92;
color4.alpha = 1.00;
color4.position = 1;
[newInstance addElement:&color1];
[newInstance addElement:&color2];
[newInstance addElement:&color3];
[newInstance addElement:&color4];
return [newInstance autorelease];
}
+ (id)aquaPressedGradient
{
id newInstance = [[[self class] alloc] init];
CTGradientElement color1;
color1.red = color1.green = color1.blue = 0.80;
color1.alpha = 1.00;
color1.position = 0;
CTGradientElement color2;
color2.red = color2.green = color2.blue = 0.64;
color2.alpha = 1.00;
color2.position = 11.5/23;
CTGradientElement color3;
color3.red = color3.green = color3.blue = 0.80;
color3.alpha = 1.00;
color3.position = 11.5/23;
CTGradientElement color4;
color4.red = color4.green = color4.blue = 0.77;
color4.alpha = 1.00;
color4.position = 1;
[newInstance addElement:&color1];
[newInstance addElement:&color2];
[newInstance addElement:&color3];
[newInstance addElement:&color4];
return [newInstance autorelease];
}
+ (id)unifiedSelectedGradient
{
id newInstance = [[[self class] alloc] init];
CTGradientElement color1;
color1.red = color1.green = color1.blue = 0.85;
color1.alpha = 1.00;
color1.position = 0;
CTGradientElement color2;
color2.red = color2.green = color2.blue = 0.95;
color2.alpha = 1.00;
color2.position = 1;
[newInstance addElement:&color1];
[newInstance addElement:&color2];
return [newInstance autorelease];
}
+ (id)unifiedNormalGradient
{
id newInstance = [[[self class] alloc] init];
CTGradientElement color1;
color1.red = color1.green = color1.blue = 0.75;
color1.alpha = 1.00;
color1.position = 0;
CTGradientElement color2;
color2.red = color2.green = color2.blue = 0.90;
color2.alpha = 1.00;
color2.position = 1;
[newInstance addElement:&color1];
[newInstance addElement:&color2];
return [newInstance autorelease];
}
+ (id)unifiedPressedGradient
{
id newInstance = [[[self class] alloc] init];
CTGradientElement color1;
color1.red = color1.green = color1.blue = 0.60;
color1.alpha = 1.00;
color1.position = 0;
CTGradientElement color2;
color2.red = color2.green = color2.blue = 0.75;
color2.alpha = 1.00;
color2.position = 1;
[newInstance addElement:&color1];
[newInstance addElement:&color2];
return [newInstance autorelease];
}
+ (id)unifiedDarkGradient
{
id newInstance = [[[self class] alloc] init];
CTGradientElement color1;
color1.red = color1.green = color1.blue = 0.68;
color1.alpha = 1.00;
color1.position = 0;
CTGradientElement color2;
color2.red = color2.green = color2.blue = 0.83;
color2.alpha = 1.00;
color2.position = 1;
[newInstance addElement:&color1];
[newInstance addElement:&color2];
return [newInstance autorelease];
}
#pragma mark -
#pragma mark Modification
- (CTGradient *)gradientWithAlphaComponent:(float)alpha
{
id newInstance = [[[self class] alloc] init];
CTGradientElement *curElement = elementList;
CTGradientElement tempElement;
while(curElement != nil)
{
tempElement = *curElement;
tempElement.alpha = alpha;
[newInstance addElement:&tempElement];
curElement = curElement->nextElement;
}
return [newInstance autorelease];
}
//Adds a color stop with <color> at <position> in elementList
//(if two elements are at the same position then added imediatly after the one that was there already)
- (CTGradient *)addColorStop:(NSColor *)color atPosition:(float)position
{
CTGradient *newGradient = [self copy];
CTGradientElement newGradientElement;
//put the components of color into the newGradientElement - must make sure it is a RGB color (not Gray or CMYK)
[[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&newGradientElement.red
green:&newGradientElement.green
blue:&newGradientElement.blue
alpha:&newGradientElement.alpha];
newGradientElement.position = position;
//Pass it off to addElement to take care of adding it to the elementList
[newGradient addElement:&newGradientElement];
return [newGradient autorelease];
}
//Removes the color stop at <position> from elementList
- (CTGradient *)removeColorStopAtPosition:(float)position
{
CTGradient *newGradient = [self copy];
CTGradientElement removedElement = [newGradient removeElementAtPosition:position];
if(isnan(removedElement.position))
[NSException raise:NSRangeException format:@"-[%@ removeColorStopAtPosition:]: no such colorStop at position (%f)", [self class], position];
return [newGradient autorelease];
}
- (CTGradient *)removeColorStopAtIndex:(unsigned)index
{
CTGradient *newGradient = [self copy];
CTGradientElement removedElement = [newGradient removeElementAtIndex:index];
if(isnan(removedElement.position))
[NSException raise:NSRangeException format:@"-[%@ removeColorStopAtIndex:]: index (%d) beyond bounds", [self class], index];
return [newGradient autorelease];
}
#pragma mark -
#pragma mark Information
//Returns color at <position> in gradient
- (NSColor *)colorStopAtIndex:(unsigned)index
{
CTGradientElement *element = [self elementAtIndex:index];
if(element != nil)
return [NSColor colorWithCalibratedRed:element->red
green:element->green
blue:element->blue
alpha:element->alpha];
[NSException raise:NSRangeException format:@"-[%@ removeColorStopAtIndex:]: index (%d) beyond bounds", [self class], index];
return nil;
}
- (NSColor *)colorAtPosition:(float)position
{
float components[4];
linearEvaluation(&elementList, &position, components);
return [NSColor colorWithCalibratedRed:components[0]
green:components[1]
blue:components[2]
alpha:components[3]];
}
#pragma mark -
#pragma mark Drawing
- (void)drawSwatchInRect:(NSRect)rect
{
[self fillRect:rect angle:45];
}
- (void)fillRect:(NSRect)rect angle:(float)angle
{
//First Calculate where the beginning and ending points should be
CGPoint startPoint;
CGPoint endPoint;
if(angle == 0 && NO) //screw the calculations - we know the answer
{
startPoint = CGPointMake(NSMinX(rect), NSMinY(rect)); //right of rect
endPoint = CGPointMake(NSMaxX(rect), NSMinY(rect)); //left of rect
}
else if(angle == 90 && NO) //same as above
{
startPoint = CGPointMake(NSMinX(rect), NSMinY(rect)); //bottom of rect
endPoint = CGPointMake(NSMinX(rect), NSMaxY(rect)); //top of rect
}
else //ok, we'll do the calculations now
{
float x,y;
float sina, cosa, tana;
float length;
float deltax,
deltay;
float rangle = angle * pi/180; //convert the angle to radians
if(fabsf(tan(rangle))<=1) //for range [-45,45], [135,225]
{
x = NSWidth(rect);
y = NSHeight(rect);
sina = sin(rangle);
cosa = cos(rangle);
tana = tan(rangle);
length = x/fabsf(cosa)+(y-x*fabsf(tana))*fabsf(sina);
deltax = length*cosa/2;
deltay = length*sina/2;
}
else //for range [45,135], [225,315]
{
x = NSHeight(rect);
y = NSWidth(rect);
sina = sin(rangle - 90*pi/180);
cosa = cos(rangle - 90*pi/180);
tana = tan(rangle - 90*pi/180);
length = x/fabsf(cosa)+(y-x*fabsf(tana))*fabsf(sina);
deltax =-length*sina/2;
deltay = length*cosa/2;
}
startPoint = CGPointMake(NSMidX(rect)-deltax, NSMidY(rect)-deltay);
endPoint = CGPointMake(NSMidX(rect)+deltax, NSMidY(rect)+deltay);
}
//Calls to CoreGraphics
CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(currentContext);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
//CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGShadingRef myCGShading = CGShadingCreateAxial(colorspace, startPoint, endPoint, gradientFunction, false, false);
CGContextClipToRect(currentContext , *(CGRect *)&rect); //This is where the action happens
CGContextDrawShading(currentContext, myCGShading);
CGShadingRelease (myCGShading);
CGColorSpaceRelease(colorspace );
CGContextRestoreGState(currentContext);
}
- (void)radialFillRect:(NSRect)rect
{
CGPoint startPoint , endPoint;
float startRadius, endRadius;
startPoint = endPoint = CGPointMake(NSMidX(rect), NSMidY(rect));
startRadius = 0;
if(NSHeight(rect)>NSWidth(rect))
endRadius = NSHeight(rect)/2;
else
endRadius = NSWidth(rect)/2;
//Calls to CoreGraphics
CGContextRef currentContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(currentContext);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
//CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGShadingRef myCGShading = CGShadingCreateRadial(colorspace, startPoint, startRadius, endPoint, endRadius, gradientFunction, true, true);
CGContextClipToRect (currentContext , *(CGRect *)&rect);
CGContextDrawShading(currentContext , myCGShading); //This is where the action happens
CGShadingRelease (myCGShading);
CGColorSpaceRelease (colorspace);
CGContextRestoreGState(currentContext);
}
#pragma mark -
#pragma mark Element List
- (void)addElement:(CTGradientElement *)newElement
{
if(elementList == nil)
{
elementList = malloc(sizeof(CTGradientElement));
*elementList = *newElement;
elementList->nextElement = nil;
}
else
{
CTGradientElement *curElement = elementList;
while(curElement->nextElement != nil && !((curElement->position <= newElement->position) && (newElement->position < curElement->nextElement->position)))
{
curElement = curElement->nextElement;
}
CTGradientElement *tmpNext = curElement->nextElement;
curElement->nextElement = malloc(sizeof(CTGradientElement));
*(curElement->nextElement) = *newElement;
curElement->nextElement->nextElement = tmpNext;
}
}
- (CTGradientElement)removeElementAtIndex:(unsigned)index
{
CTGradientElement removedElement;
removedElement.red = 0.0;
removedElement.green = 0.0;
removedElement.blue = 0.0;
removedElement.alpha = 0.0;
removedElement.position = NAN;
removedElement.nextElement = NULL;
if(elementList != nil)
{
if(index == 0)
{
CTGradientElement *tmpNext = elementList;
elementList = elementList->nextElement;
removedElement = *tmpNext;
free(tmpNext);
return removedElement;
}
unsigned count = 1; //we want to start one ahead
CTGradientElement *currentElement = elementList;
while(currentElement->nextElement != nil)
{
if(count == index)
{
CTGradientElement *tmpNext = currentElement->nextElement;
currentElement->nextElement = currentElement->nextElement->nextElement;
removedElement = *tmpNext;
free(tmpNext);
return removedElement;
}
count++;
currentElement = currentElement->nextElement;
}
}
removedElement.position = NAN;
return removedElement;
}
- (CTGradientElement)removeElementAtPosition:(float)position
{
CTGradientElement removedElement;
removedElement.red = 0.0;
removedElement.green = 0.0;
removedElement.blue = 0.0;
removedElement.alpha = 0.0;
removedElement.position = NAN;
removedElement.nextElement = NULL;
if(elementList != nil)
{
if(elementList->position == position)
{
CTGradientElement *tmpNext = elementList;
elementList = elementList->nextElement;
removedElement = *tmpNext;
free(tmpNext);
return removedElement;
}
else
{
CTGradientElement *curElement = elementList;
while(curElement->nextElement != nil)
{
if(curElement->nextElement->position == position)
{
CTGradientElement *tmpNext = curElement->nextElement;
curElement->nextElement = curElement->nextElement->nextElement;
removedElement = *tmpNext;
free(tmpNext);
return removedElement;
}
}
}
}
removedElement.position = NAN;
return removedElement;
}
- (CTGradientElement *)elementAtIndex:(unsigned)index;
{
unsigned count = 0;
CTGradientElement *currentElement = elementList;
while(currentElement != nil)
{
if(count == index)
return currentElement;
count++;
currentElement = currentElement->nextElement;
}
return nil;
}
#pragma mark -
#pragma mark Core Graphics
- (CGFunctionRef)buildCGFunction
{
static const float input_value_range [2] = { 0, 1 }; //range for the evaluator input
static const float output_value_ranges [8] = { 0, 1, 0, 1, 0, 1, 0, 1 }; //ranges for the evaluator output (4 returned values)
return CGFunctionCreate(&elementList, //the two transition colors
1, input_value_range , //number of inputs (just fraction of progression)
4, output_value_ranges, //number of outputs RGBa
&_CTLinearGradientFunction); //info for using the evaluator funtion
}
//////////////////////////////////////LinearEvaluation Function/////////////////////////////////////
void linearEvaluation (void *info, const float *in, float *out)
{
float position = *in;
if(*(CTGradientElement **)info == nil) //if elementList is empty return clear color
{
out[0] = out[1] = out[2] = out[3] = 0;
return;
}
//This grabs the first two colors in the sequence
CTGradientElement *color1 = *(CTGradientElement **)info;
CTGradientElement *color2 = color1->nextElement;
//make sure first color and second color are on other sides of position
while(color2 != nil && color2->position < position)
{
color1 = color2;
color2 = color1->nextElement;
}
//if we don't have another color then make next color the same color
if(color2 == nil)
{
color2 = color1;
}
//----------FailSafe settings----------
//color1->red = 1; color2->red = 0;
//color1->green = 1; color2->green = 0;
//color1->blue = 1; color2->blue = 0;
//color1->alpha = 1; color2->alpha = 1;
//color1->position = .5;
//color2->position = .5;
//-------------------------------------
if(position <= color1->position) //Make all below color color1's position equal to color1
{
out[0] = color1->red;
out[1] = color1->green;
out[2] = color1->blue;
out[3] = color1->alpha;
}
else if (position >= color2->position) //Make all above color color2's position equal to color2
{
out[0] = color2->red;
out[1] = color2->green;
out[2] = color2->blue;
out[3] = color2->alpha;
}
else //Interpolate color at postions between color1 and color1
{
//adjust position so that it goes from 0 to 1 in the range from color 1 & 2's position
position = (position-color1->position)/(color2->position - color1->position);
out[0] = (color2->red - color1->red )*position + color1->red;
out[1] = (color2->green - color1->green)*position + color1->green;
out[2] = (color2->blue - color1->blue )*position + color1->blue;
out[3] = (color2->alpha - color1->alpha)*position + color1->alpha;
}
}
@end