1/*
2    PPMiniColorWell.m
3
4    Copyright 2013-2018 Josh Freeman
5    http://www.twilightedge.com
6
7    This file is part of PikoPixel for Mac OS X and GNUstep.
8    PikoPixel is a graphical application for drawing & editing pixel-art images.
9
10    PikoPixel is free software: you can redistribute it and/or modify it under
11    the terms of the GNU Affero General Public License as published by the
12    Free Software Foundation, either version 3 of the License, or (at your
13    option) any later version approved for PikoPixel by its copyright holder (or
14    an authorized proxy).
15
16    PikoPixel is distributed in the hope that it will be useful, but WITHOUT ANY
17    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18    FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19    details.
20
21    You should have received a copy of the GNU Affero General Public License
22    along with this program. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#import "PPMiniColorWell.h"
26
27#import "PPGeometry.h"
28
29
30@interface PPMiniColorWell (PrivateMethods)
31
32- (void) drawTransparencyBackgroundInBounds: (NSRect) bounds;
33
34@end
35
36@implementation PPMiniColorWell
37
38- (void) dealloc
39{
40    [_fillColor release];
41    [_outlineColor release];
42
43    [super dealloc];
44}
45
46- (void) setColor: (NSColor *) color
47{
48    if (_fillColor == color)
49    {
50        return;
51    }
52
53    [_fillColor release];
54    _fillColor = [color retain];
55
56    _fillColorIsOpaque = ([color alphaComponent] >= 1.0f) ? YES : NO;
57
58    [self setNeedsDisplay: YES];
59}
60
61- (void) setOutlineColor: (NSColor *) color
62{
63    if (_outlineColor == color)
64    {
65        return;
66    }
67
68    [_outlineColor release];
69    _outlineColor = [color retain];
70
71    [self setNeedsDisplay: YES];
72}
73
74#pragma mark NSView overrides
75
76- (void) drawRect: (NSRect) rect
77{
78    NSRect viewDrawBounds = PPGeometry_PixelCenteredRect([self bounds]);
79
80    if (_fillColor)
81    {
82        if (!_fillColorIsOpaque)
83        {
84            [self drawTransparencyBackgroundInBounds: viewDrawBounds];
85        }
86
87        [_fillColor set];
88        [NSBezierPath fillRect: viewDrawBounds];
89    }
90
91    if (_outlineColor)
92    {
93        [_outlineColor set];
94        [NSBezierPath strokeRect: viewDrawBounds];
95
96        if (!_fillColor)
97        {
98            NSBezierPath *xPath = [NSBezierPath bezierPath];
99
100            [xPath moveToPoint: viewDrawBounds.origin];
101            [xPath lineToPoint: NSMakePoint(viewDrawBounds.origin.x + viewDrawBounds.size.width,
102                                        viewDrawBounds.origin.y + viewDrawBounds.size.height)];
103
104            [xPath moveToPoint: NSMakePoint(viewDrawBounds.origin.x,
105                                        viewDrawBounds.origin.y + viewDrawBounds.size.height)];
106            [xPath lineToPoint: NSMakePoint(viewDrawBounds.origin.x + viewDrawBounds.size.width,
107                                        viewDrawBounds.origin.y)];
108
109            [xPath stroke];
110        }
111    }
112}
113
114#pragma mark Private methods
115
116- (void) drawTransparencyBackgroundInBounds: (NSRect) bounds
117{
118    NSBezierPath *trianglePath = [NSBezierPath bezierPath];
119
120    [trianglePath moveToPoint: bounds.origin];
121    [trianglePath lineToPoint: NSMakePoint(bounds.origin.x + bounds.size.width,
122                                            bounds.origin.y + bounds.size.height)];
123    [trianglePath lineToPoint: NSMakePoint(bounds.origin.x + bounds.size.width,
124                                            bounds.origin.y)];
125    [trianglePath closePath];
126
127    [[NSColor blackColor] set];
128    NSRectFill(bounds);
129
130    [[NSColor whiteColor] set];
131    [trianglePath fill];
132}
133
134@end
135