File: NSGradient.m

package info (click to toggle)
gnustep-gui 0.25.0-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,088 kB
  • ctags: 3,417
  • sloc: objc: 153,800; ansic: 18,239; cpp: 579; yacc: 462; makefile: 143; sh: 5
file content (389 lines) | stat: -rw-r--r-- 10,557 bytes parent folder | download | duplicates (4)
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
/*
   NSGradient.m

   GUI implementation of a colour gradient.

   Copyright (C) 2009 Free Software Foundation, Inc.

   Author: Fred Kiefer <fredkiefer@gmx.de>
   Date: Oct 2009
   Author: H. Nikolaus Schaller <hns@computer.org>
   Date: Dec 2007
   
   This file is part of the GNUstep GUI Library.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; see the file COPYING.LIB.
   If not, see <http://www.gnu.org/licenses/> or write to the 
   Free Software Foundation, 51 Franklin Street, Fifth Floor, 
   Boston, MA 02110-1301, USA.
*/ 

#import <Foundation/NSException.h>
#import "AppKit/NSBezierPath.h"
#import "AppKit/NSColor.h"
#import "AppKit/NSColorSpace.h"
#import "AppKit/NSGradient.h"
#import "AppKit/NSGraphicsContext.h"

#include <math.h>

#ifndef PI
#define PI 3.1415926535897932384626434
#endif

@interface NSGradient (Private)
- (void) _drawInRect: (NSRect)rect angle: (CGFloat)angle;
- (void) _drawInRect: (NSRect)rect 
relativeCenterPosition: (NSPoint)relativeCenterPoint;
@end

@implementation NSGradient

- (NSColorSpace *) colorSpace; 
{
  return _colorSpace;
}

- (void) drawFromCenter: (NSPoint)startCenter
                 radius: (CGFloat)startRadius
               toCenter: (NSPoint)endCenter 
                 radius: (CGFloat)endRadius
                options: (NSGradientDrawingOptions)options
{
  [[NSGraphicsContext currentContext] drawGradient: self
                                      fromCenter: startCenter
                                      radius: startRadius
                                      toCenter: endCenter 
                                      radius: endRadius
                                      options: options];
}

- (void) drawFromPoint: (NSPoint)startPoint
               toPoint: (NSPoint)endPoint
               options: (NSGradientDrawingOptions)options
{
  [[NSGraphicsContext currentContext] drawGradient: self
                                      fromPoint: startPoint
                                      toPoint: endPoint
                                      options: options];
}

- (void) drawInBezierPath: (NSBezierPath *)path angle: (CGFloat)angle
{
  NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];

  [currentContext saveGraphicsState];
  [path addClip];
  [self _drawInRect: [path bounds] angle: angle];
  [currentContext restoreGraphicsState];
}

- (void) drawInBezierPath: (NSBezierPath *)path
   relativeCenterPosition: (NSPoint)relativeCenterPoint
{
  NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];

  [currentContext saveGraphicsState];
  [path addClip];
  [self _drawInRect: [path bounds] relativeCenterPosition: relativeCenterPoint];
  [currentContext restoreGraphicsState];
}

- (void) drawInRect: (NSRect)rect angle: (CGFloat)angle
{
  NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];

  [currentContext saveGraphicsState];
  [NSBezierPath clipRect: rect];
  [self _drawInRect: rect angle: angle];
  [currentContext restoreGraphicsState];
}

- (void) drawInRect: (NSRect)rect 
relativeCenterPosition: (NSPoint)relativeCenterPoint
{
  NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];

  [currentContext saveGraphicsState];
  [NSBezierPath clipRect: rect];
  [self _drawInRect: rect relativeCenterPosition: relativeCenterPoint];
  [currentContext restoreGraphicsState];
}

- (void) getColor: (NSColor **)color
         location: (CGFloat *)location
          atIndex: (NSInteger)index
{
  NSAssert(index >= 0 && index < _numberOfColorStops, @"NSGradient invalid index");

  if (color)
    *color = [_colors objectAtIndex: index];
  if (location)
    *location = _locations[index];
}

- (id) initWithColors: (NSArray *)colorArray;
{
  return [self initWithColors: colorArray 
               atLocations: NULL
               colorSpace: nil];
}

- (id) initWithColors: (NSArray *)colorArray
          atLocations: (const CGFloat *)locations
           colorSpace: (NSColorSpace *)colorSpace;
{
  if ((self = [super init]))
    {
      _numberOfColorStops = [colorArray count];
      NSAssert(_numberOfColorStops >= 2, @"NSGradient needs at least 2 locations");
      if (colorSpace == nil)
        {
          colorSpace = [[colorArray objectAtIndex: 0] colorSpace];
        }
      ASSIGN(_colorSpace, colorSpace);
      
      // FIXME: Convert all colours to colour space
      ASSIGN(_colors, colorArray);

      _locations = malloc(sizeof(CGFloat)*_numberOfColorStops);
      if (locations)
        {
          // FIXME: Check that the locations are properly ordered
          memcpy(_locations, locations, sizeof(CGFloat) * _numberOfColorStops);
        }
      else
        {
          unsigned int i;
          
          // evenly spaced
          for (i = 0; i < _numberOfColorStops; i++)
            _locations[i] = (float)i / (_numberOfColorStops - 1);
        }
    }
  return self;
}

- (id) initWithColorsAndLocations: (NSColor *)color, ...
{
  va_list ap;
  unsigned int max = 128;
  unsigned int count = 0;
  CGFloat *locations = (CGFloat*)malloc(max * sizeof(CGFloat));
  NSMutableArray *colorArray = [[NSMutableArray alloc] init];

  va_start(ap, color);
  while (color != nil)
    {
      if (max <= count)
        {
          max *= 2;
          locations = (CGFloat*)realloc(locations, max * sizeof(CGFloat));
        }
      [colorArray addObject: color];
      // gcc insists on using double here
      locations[count++] = (CGFloat)va_arg(ap, double);

      color = va_arg(ap, id);
    }
  va_end(ap);

  self = [self initWithColors: colorArray
               atLocations: locations
               colorSpace: nil];

  RELEASE(colorArray);
  free(locations);
  return self;
}

- (id) initWithStartingColor: (NSColor *)startColor endingColor: (NSColor *)endColor
{
  return [self initWithColors: [NSArray arrayWithObjects: startColor, endColor, nil]];
}

- (void) dealloc
{
  RELEASE(_colorSpace);
  RELEASE(_colors);
  free(_locations);
  [super dealloc];
}

- (NSColor *) interpolatedColorAtLocation: (CGFloat)location
{
  unsigned int i;

  if (location <= _locations[0])
    {
      return [_colors objectAtIndex: 0];
    }

  if (location >= _locations[_numberOfColorStops - 1])
    {
      return [_colors objectAtIndex: _numberOfColorStops - 1];
    }

  for (i = 1; i < _numberOfColorStops; i++)
    {
      if (location <= _locations[i])
        {
          NSColor *c1 = [_colors objectAtIndex: i - 1];
          NSColor *c2 = [_colors objectAtIndex: i];
          float fraction = (_locations[i] - location) / (_locations[i] - _locations[i - 1]);
          
          // FIXME: Works only for RGB colours and does not respect the colour space
          return [c1 blendedColorWithFraction: fraction
                     ofColor: c2];
        }
    }

  return nil;
}

- (NSInteger) numberOfColorStops
{
  return _numberOfColorStops;
}

/*
 * Copying
 */
- (id) copyWithZone: (NSZone*)zone
{
  NSGradient *g = (NSGradient*)NSCopyObject(self, 0, zone);

  RETAIN(g->_colorSpace);
  RETAIN(g->_colors);
  g->_locations = malloc(sizeof(CGFloat) * _numberOfColorStops);
  memcpy(g->_locations, _locations, sizeof(CGFloat) * _numberOfColorStops);

  return g;
}

/*
 * NSCoding protocol
 */
- (void) encodeWithCoder: (NSCoder*)aCoder
{
  if ([aCoder allowsKeyedCoding])
    {
    }
  else
    {
    }
}

- (id) initWithCoder: (NSCoder*)aDecoder
{
  if ([aDecoder allowsKeyedCoding])
    {
    }
  else
    {
    }
  return self;
}

@end

@implementation NSGradient (Private)

- (void) _drawInRect: (NSRect)rect angle: (CGFloat)angle
{
  NSPoint startPoint;
  NSPoint endPoint;
  float rad;
  float length;

  // Normalize to 0.0 <= angle <= 360.0
  while (angle < 0.0)
    {
      angle += 360.0;
    }
  while (angle > 360.0)
    {
      angle -= 360.0;
    }

  if (angle < 90.0)
    {
      startPoint = NSMakePoint(NSMinX(rect), NSMinY(rect));
    }
  else if (angle < 180.0)
    {
      startPoint = NSMakePoint(NSMaxX(rect), NSMinY(rect));
    }
  else if (angle < 270.0)
    {
      startPoint = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
    }
  else
    {
      startPoint = NSMakePoint(NSMinX(rect), NSMaxY(rect));
    }
  rad = PI * angle / 180;
  length = fabs(NSWidth(rect) * cos(rad) + NSHeight(rect) * sin(rad));
  endPoint = NSMakePoint(startPoint.x + length * cos(rad), 
                         startPoint.y + length * sin(rad));

  [self  drawFromPoint: startPoint
         toPoint: endPoint
         options: 0];
}

static inline float sqr(float a)
{
  return a * a;
}

static inline float euclidian_distance(NSPoint start, NSPoint end)
{
  return sqrt(sqr(end.x - start.x) + sqr(end.y - start.y));
}

- (void) _drawInRect: (NSRect)rect 
relativeCenterPosition: (NSPoint)relativeCenterPoint
{
  NSPoint startCenter;
  NSPoint endCenter;
  CGFloat endRadius;
  CGFloat distance;

  NSAssert(relativeCenterPoint.x >= 0.0 && relativeCenterPoint.x <= 1.0, @"NSGradient invalid relative center point");
  NSAssert(relativeCenterPoint.y >= 0.0 && relativeCenterPoint.y <= 1.0, @"NSGradient invalid relative center point");
  startCenter = NSMakePoint(NSMidX(rect), NSMidY(rect));
  endCenter = NSMakePoint(startCenter.x + rect.size.width * relativeCenterPoint.x, 
                          startCenter.y + rect.size.height * relativeCenterPoint.y);
  endRadius = 0.0;
  distance = euclidian_distance(endCenter, NSMakePoint(NSMinX(rect), NSMinY(rect)));
  if (endRadius < distance)
    endRadius = distance;
  distance = euclidian_distance(endCenter, NSMakePoint(NSMaxX(rect), NSMinY(rect)));
  if (endRadius < distance)
    endRadius = distance;
  distance = euclidian_distance(endCenter, NSMakePoint(NSMinX(rect), NSMaxY(rect)));
  if (endRadius < distance)
    endRadius = distance;
  distance = euclidian_distance(endCenter, NSMakePoint(NSMaxX(rect), NSMaxY(rect)));
  if (endRadius < distance)
    endRadius = distance;

  [self drawFromCenter: startCenter
        radius: 0.0
        toCenter: endCenter 
        radius: endRadius
        options: 0];
}
@end