forked from alejandrojapkin/CocoaLUT
/
LUT1DGraphView.m
401 lines (310 loc) · 15.7 KB
/
LUT1DGraphView.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
//
// LUT1DGraphView.m
// Pods
//
// Created by Greg Cotten on 5/1/14.
//
//
#import "LUT1DGraphView.h"
#define MAX_GRID_POINTS 64
@interface LUT1DGraphView ()
@property (assign) double minimumOutputValue;
@property (assign) double maximumOutputValue;
@end
@implementation LUT1DGraphViewController
-(void)awakeFromNib{
[super awakeFromNib];
[self initialize];
}
- (void)initialize{
[self.view addObserver:self forKeyPath:@"mousePoint" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)dealloc{
[self.view removeObserver:self forKeyPath:@"mousePoint"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if([keyPath isEqualToString:@"mousePoint"]){
[self mouseMoved];
}
}
- (void)mouseMoved{
NSArray *indexLUTColorAndIdentityLUTColor = [((LUT1DGraphView *)self.view) indexLUTColorAndIdentityLUTColorFromCurrentMousePoint];
//double index = [indexLUTColorAndIdentityLUTColor[0] doubleValue];
LUTColor *color = indexLUTColorAndIdentityLUTColor[1];
LUTColor *identityColor = indexLUTColorAndIdentityLUTColor[2];
self.colorizedColorStringAtMousePoint = [[self class] colorizedColorTransformationStringFromStartColor:identityColor endColor:color];
self.inputColor = identityColor.systemColor;
self.outputColor = color.systemColor;
}
- (void)setViewWithLUT:(LUT1D *)lut{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
((LUT1DGraphView *)self.view).lut = lut;
});
}
+ (NSAttributedString *)colorizedColorTransformationStringFromStartColor:(LUTColor *)startColor
endColor:(LUTColor *)endColor{
NSMutableAttributedString *outString = [[NSMutableAttributedString alloc] initWithAttributedString:[startColor colorizedAttributedStringWithFormat:@"%.6f"]];
[outString appendAttributedString:[[NSAttributedString alloc] initWithString:@" ‣ "]];
[outString appendAttributedString:[endColor colorizedAttributedStringWithFormat:@"%.6f"]];
[outString addAttribute:NSFontAttributeName value:[NSFont systemFontOfSize:[NSFont systemFontSize]] range:NSMakeRange(0, outString.length)];
return outString;
}
- (void)setInterpolation:(LUT1DGraphViewInterpolation)interpolation{
((LUT1DGraphView *)self.view).interpolation = interpolation;
}
@end
@implementation LUT1DGraphView
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self initialize];
}
return self;
}
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
-(void)initialize{
self.interpolation = LUT1DGraphViewInterpolationLinear;
self.currentTrackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
options: (NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow | NSTrackingInVisibleRect )
owner:self userInfo:nil];
[self addTrackingArea:self.currentTrackingArea];
self.mouseIsIn = NO;
}
-(void)mouseMoved:(NSEvent *)theEvent{
self.mousePoint = theEvent.locationInWindow;
[self setNeedsDisplay:YES];
}
-(void)mouseEntered:(NSEvent *)theEvent{
self.mouseIsIn = YES;
}
-(void)mouseExited:(NSEvent *)theEvent{
self.mouseIsIn = NO;
[self setNeedsDisplay:YES];
}
-(void)setLut:(LUT1D *)lut{
_lut = lut;
[self lutDidChange];
}
-(void)setInterpolation:(LUT1DGraphViewInterpolation)interpolation{
if(interpolation != _interpolation){
_interpolation = interpolation;
[self setNeedsDisplay:YES];
}
//no need to redraw if the interpolation didn't actually change!
}
-(BOOL)isOpaque {
return YES;
}
-(void)lutDidChange{
if(self.lut){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.minimumOutputValue = [self.lut minimumOutputValue];
self.maximumOutputValue = [self.lut maximumOutputValue];
dispatch_async(dispatch_get_main_queue(), ^{
[self setNeedsDisplay:YES];
});
});
}
}
- (void)drawRect:(NSRect)dirtyRect {
[[NSColor whiteColor] setFill];
NSRectFill(self.bounds);
NSRect drawingRect = self.bounds;
if(!self.lut){
return;
}
CGContextRef context = [[NSGraphicsContext
currentContext] graphicsPort];
[self drawGridInContext:context inRect:drawingRect numXDivs: MIN((int)self.lut.size, MAX_GRID_POINTS) transparency:.2];
if(![self.lut equalsIdentityLUT]){
[self drawIdentityLineInContext:context inRect:drawingRect thickness:2.0 transparency:.4];
}
if(self.interpolation == LUT1DGraphViewInterpolationLinear){
[self drawLUT1D:self.lut inContext:context inRect:drawingRect thickness:2.0];
}
if(self.mouseIsIn){
[self drawOverlayInContext:context inRect:drawingRect withPoint:self.mousePoint thickness:2.0 opacity:.8];
}
}
- (void)drawOverlayInContext:(CGContextRef)context
inRect:(NSRect)rect
withPoint:(NSPoint)point
thickness:(double)thickness
opacity:(double)opacity{
CGFloat xOrigin = rect.origin.x;
CGFloat yOrigin = rect.origin.y;
CGFloat pixelWidth = rect.size.width;
CGFloat pixelHeight = rect.size.height;
double xPosition = round(point.x);
if(outOfBounds(xPosition, xOrigin, xOrigin+pixelWidth, YES)){
return;
}
double xPositionAsInterpolatedIndex = remap(xPosition, xOrigin, pixelWidth - xOrigin, 0, self.lut.size - 1);
LUTColor *colorAtXPosition = [self.lut colorAtInterpolatedR:xPositionAsInterpolatedIndex g:xPositionAsInterpolatedIndex b:xPositionAsInterpolatedIndex];
double redYPosition = round(remap(colorAtXPosition.red,
clampUpperBound(self.minimumOutputValue, 0),
clampLowerBound(self.maximumOutputValue, 1),
yOrigin,
yOrigin + pixelHeight));
double greenYPosition = round(remap(colorAtXPosition.green,
clampUpperBound(self.minimumOutputValue, 0),
clampLowerBound(self.maximumOutputValue, 1),
yOrigin,
yOrigin + pixelHeight));
double blueYPosition = round(remap(colorAtXPosition.blue,
clampUpperBound(self.minimumOutputValue, 0),
clampLowerBound(self.maximumOutputValue, 1),
yOrigin,
yOrigin + pixelHeight));
// CGContextSetRGBFillColor(context, 1, 0, 0, 1);
// CGContextFillEllipseInRect(context, CGRectMake(xPosition-(1.0/2.0)*thickness, redYPosition-(1.0/2.0)*thickness, thickness, thickness));
//
// CGContextSetRGBFillColor(context, 0, 1, 0, 1);
// CGContextFillEllipseInRect(context, CGRectMake(xPosition-(1.0/2.0)*thickness, greenYPosition-(1.0/2.0)*thickness, thickness, thickness));
//
// CGContextSetRGBFillColor(context, 0, 0, 1, 1);
// CGContextFillEllipseInRect(context, CGRectMake(xPosition-(1.0/2.0)*thickness, blueYPosition-(1.0/2.0)*thickness, thickness, thickness));
CGContextSetLineWidth(context, thickness);
CGContextSetRGBStrokeColor(context, 0, 0, 0, opacity*.5);
CGContextBeginPath(context);
CGContextMoveToPoint(context, xPosition, yOrigin);
CGContextAddLineToPoint(context, xPosition, yOrigin+pixelHeight);
CGContextStrokePath(context);
CGContextSetRGBStrokeColor(context, 1.0, 0, 0, opacity);
CGContextBeginPath(context);
CGContextMoveToPoint(context, xOrigin, redYPosition);
CGContextAddLineToPoint(context, xOrigin + pixelWidth, redYPosition);
CGContextStrokePath(context);
CGContextSetRGBStrokeColor(context, 0, 1.0, 0, opacity);
CGContextBeginPath(context);
CGContextMoveToPoint(context, xOrigin, greenYPosition);
CGContextAddLineToPoint(context, xOrigin + pixelWidth, greenYPosition);
CGContextStrokePath(context);
CGContextSetRGBStrokeColor(context, 0, 0, 1.0, opacity);
CGContextBeginPath(context);
CGContextMoveToPoint(context, xOrigin, blueYPosition);
CGContextAddLineToPoint(context, xOrigin + pixelWidth, blueYPosition);
CGContextStrokePath(context);
}
- (NSArray *)indexLUTColorAndIdentityLUTColorFromCurrentMousePoint{
CGFloat xOrigin = self.bounds.origin.x;
CGFloat pixelWidth = self.bounds.size.width;
double xPosition = clamp(self.mousePoint.x, 0, pixelWidth);
double xPositionAsInterpolatedIndex = remapNoError(xPosition, xOrigin, pixelWidth - xOrigin, 0, self.lut.size - 1);
LUTColor *colorAtXPosition = [self.lut colorAtInterpolatedR:xPositionAsInterpolatedIndex
g:xPositionAsInterpolatedIndex
b:xPositionAsInterpolatedIndex];
LUTColor *identityColorAtXPosition = [self.lut identityColorAtR:xPositionAsInterpolatedIndex
g:xPositionAsInterpolatedIndex
b:xPositionAsInterpolatedIndex];
return @[@(xPositionAsInterpolatedIndex), colorAtXPosition, identityColorAtXPosition];
}
- (void)drawIdentityLineInContext:(CGContextRef)context
inRect:(NSRect)rect
thickness:(double)thickness
transparency:(double)transparency{
CGFloat xOrigin = rect.origin.x;
CGFloat yOrigin = rect.origin.y;
CGFloat pixelWidth = rect.size.width;
CGFloat pixelHeight = rect.size.height;
CGContextSetRGBStrokeColor(context, 1.0-transparency, 1.0-transparency, 1.0-transparency, 1);
CGFloat strokeWidth = 1.0;
CGContextSetLineWidth(context, strokeWidth);
CGContextBeginPath(context);
CGContextMoveToPoint(context, xOrigin, yOrigin);
CGContextAddLineToPoint(context, xOrigin+pixelWidth, yOrigin + pixelHeight);
CGContextStrokePath(context);
}
- (void)drawGridInContext:(CGContextRef)context
inRect:(NSRect)rect
numXDivs:(int)numDivs
transparency:(double)transparency{
CGFloat xOrigin = rect.origin.x;
CGFloat yOrigin = rect.origin.y;
CGFloat pixelWidth = rect.size.width;
CGFloat pixelHeight = rect.size.height;
NSArray *xIndices = indicesDoubleArray(xOrigin, xOrigin + pixelWidth, numDivs);
CGContextSetRGBStrokeColor(context, 1.0-transparency, 1.0-transparency, 1.0-transparency, 1);
CGFloat strokeWidth = 1.0 / [[self window] backingScaleFactor];
CGContextSetLineWidth(context, strokeWidth);
for (NSNumber *index in xIndices){
double indexAsDouble = [index doubleValue];
CGContextBeginPath(context);
CGContextMoveToPoint(context, indexAsDouble, yOrigin);
CGContextAddLineToPoint(context, indexAsDouble, yOrigin + pixelHeight);
CGContextStrokePath(context);
}
NSArray *yIndices = indicesDoubleArray(yOrigin, yOrigin + pixelHeight, numDivs);
for (NSNumber *index in yIndices){
double indexAsDouble = [index doubleValue];
CGContextBeginPath(context);
CGContextMoveToPoint(context, xOrigin, indexAsDouble);
CGContextAddLineToPoint(context, xOrigin + pixelWidth, indexAsDouble);
CGContextStrokePath(context);
}
}
- (void)drawLUT1D:(LUT1D *)lut1D inContext:(CGContextRef)context inRect:(NSRect)rect thickness:(double)thickness {
CGFloat xOrigin = rect.origin.x;
CGFloat yOrigin = rect.origin.y;
CGFloat pixelWidth = rect.size.width;
CGFloat pixelHeight = rect.size.height;
//NSLog(@"Drawing in %f %f", pixelWidth, pixelHeight);
CGMutablePathRef redPath = CGPathCreateMutable();
CGMutablePathRef greenPath = CGPathCreateMutable();
CGMutablePathRef bluePath = CGPathCreateMutable();
[self.lut LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
double indexAsXPixel = remap(r, 0, self.lut.size - 1, xOrigin, pixelWidth - xOrigin);
LUTColor *color = [self.lut colorAtR:r g:g b:b];
double redYMapped = remapNoError(color.red, clampUpperBound(self.minimumOutputValue, 0), clampLowerBound(self.maximumOutputValue, 1), yOrigin, yOrigin + pixelHeight);
double greenYMapped = remapNoError(color.green, clampUpperBound(self.minimumOutputValue, 0), clampLowerBound(self.maximumOutputValue, 1), yOrigin, yOrigin + pixelHeight);
double blueYMapped = remapNoError(color.blue, clampUpperBound(self.minimumOutputValue, 0), clampLowerBound(self.maximumOutputValue, 1), yOrigin, yOrigin + pixelHeight);
if (r == 0) {
CGPathMoveToPoint(redPath, nil, indexAsXPixel, redYMapped);
CGPathMoveToPoint(greenPath, nil, indexAsXPixel, greenYMapped);
CGPathMoveToPoint(bluePath, nil, indexAsXPixel, blueYMapped);
} else {
CGPathAddLineToPoint(redPath, nil, indexAsXPixel, redYMapped);
CGPathAddLineToPoint(greenPath, nil, indexAsXPixel, greenYMapped);
CGPathAddLineToPoint(bluePath, nil, indexAsXPixel, blueYMapped);
}
}];
CGContextSetLineWidth(context, thickness);
CGContextSetRGBStrokeColor(context, 1, 0, 0, 1);
CGContextAddPath(context, redPath);
CGContextStrokePath(context);
CGContextSetRGBStrokeColor(context, 0, 1, 0, 1);
CGContextAddPath(context, greenPath);
CGContextStrokePath(context);
CGContextSetRGBStrokeColor(context, 0, 0, 1, 1);
CGContextAddPath(context, bluePath);
CGContextStrokePath(context);
CGPathRelease(redPath);
CGPathRelease(greenPath);
CGPathRelease(bluePath);
if(self.lut.size <= MAX_GRID_POINTS){
[self.lut LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
double indexAsXPixel = remap(r, 0, self.lut.size - 1, xOrigin, pixelWidth - xOrigin);
LUTColor *color = [self.lut colorAtR:r g:g b:b];
double redYMapped = remapNoError(color.red, clampUpperBound(self.minimumOutputValue, 0), clampLowerBound(self.maximumOutputValue, 1), yOrigin, yOrigin + pixelHeight);
double greenYMapped = remapNoError(color.green, clampUpperBound(self.minimumOutputValue, 0), clampLowerBound(self.maximumOutputValue, 1), yOrigin, yOrigin + pixelHeight);
double blueYMapped = remapNoError(color.blue, clampUpperBound(self.minimumOutputValue, 0), clampLowerBound(self.maximumOutputValue, 1), yOrigin, yOrigin + pixelHeight);
CGContextSetRGBFillColor(context, 1, 0, 0, 1);
CGContextFillEllipseInRect(context, CGRectMake(indexAsXPixel-(3.0/2.0)*thickness, redYMapped-(3.0/2.0)*thickness, 3.0*thickness, 3.0*thickness));
CGContextSetRGBFillColor(context, 0, 1, 0, 1);
CGContextFillEllipseInRect(context, CGRectMake(indexAsXPixel-(3.0/2.0)*thickness, greenYMapped-(3.0/2.0)*thickness, 3.0*thickness, 3.0*thickness));
CGContextSetRGBFillColor(context, 0, 0, 1, 1);
CGContextFillEllipseInRect(context, CGRectMake(indexAsXPixel-(3.0/2.0)*thickness, blueYMapped-(3.0/2.0)*thickness, 3.0*thickness, 3.0*thickness));
}];
}
}
+ (M13OrderedDictionary *)interpolationMethods{
return M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"Linear": @(LUT1DGraphViewInterpolationLinear)}]);
}
@end