Repository navigation
Expand file tree
/
Copy pathWDGradientStop.m
More file actions
120 lines (93 loc) · 2.65 KB
/
Copy pathWDGradientStop.m
File metadata and controls
120 lines (93 loc) · 2.65 KB
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
//
// WDGradientStop.m
// Inkpad
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2000-2013 Steve Sprang
//
#import "WDGradientStop.h"
#import "WDColor.h"
#import "WDUtilities.h"
#import "WDXMLElement.h"
NSString *WDStopRatioKey = @"WDStopRatioKey";
NSString *WDStopColorKey = @"WDStopColorKey";
@implementation WDGradientStop
@synthesize ratio = ratio_;
@synthesize color = color_;
+ (WDGradientStop *) stopWithColor:(WDColor *)color andRatio:(float)ratio
{
WDGradientStop *stop = [[WDGradientStop alloc] initWithColor:color andRatio:ratio];
return stop;
}
- (id) initWithColor:(WDColor *)color andRatio:(float)ratio
{
self = [super init];
if (!self) {
return nil;
}
color_ = color;
ratio_ = WDClamp(0.0f, 1.0f, ratio);
return self;
}
- (NSComparisonResult) compare:(WDGradientStop *)stop
{
if (self.ratio < stop.ratio) {
return NSOrderedAscending;
} else if (self.ratio > stop.ratio) {
return NSOrderedDescending;
}
return NSOrderedSame;
}
- (WDGradientStop *) stopWithRatio:(float)ratio
{
return [WDGradientStop stopWithColor:self.color andRatio:ratio];
}
- (WDGradientStop *) stopWithColor:(WDColor *)color
{
return [WDGradientStop stopWithColor:color andRatio:self.ratio];
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeFloat:ratio_ forKey:WDStopRatioKey];
[coder encodeObject:color_ forKey:WDStopColorKey];
}
- (id)initWithCoder:(NSCoder *)coder
{
self = [super init];
ratio_ = [coder decodeFloatForKey:WDStopRatioKey];
color_ = [coder decodeObjectForKey:WDStopColorKey];
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (NSString *) description
{
return [NSString stringWithFormat:@"%@: ratio: %f color: %@", [super description], ratio_, [color_ description]];
}
- (BOOL) isEqual:(WDGradientStop *)stop
{
// test equality for each ivar, fastest to slowest
if (self.ratio != stop.ratio) {
return NO;
}
if (![self.color isEqual:stop.color]) {
return NO;
}
return YES;
}
- (WDXMLElement *) SVGXMLElement
{
WDXMLElement *stop = [WDXMLElement elementWithName:@"stop"];
[stop setAttribute:@"offset" value:[NSString stringWithFormat:@"%g", ratio_]];
[stop setAttribute:@"stop-color" value:color_.hexValue];
if (color_.alpha != 1.0) {
[stop setAttribute:@"stop-opacity" value:[NSString stringWithFormat:@"%g", color_.alpha]];
}
return stop;
}
@end