文件
dayu-color/大鱼调色/Plugin/DYViewerGraphics.inc.h
T

72 行
9.9 KiB
C++

// Methods of DYViewerParameterPanel. Geometry is shared by drawing and input.
- (double)graphicValue:(NSString*)key{return [self valueForDescriptor:[self descriptorForKey:key]];}
- (NSArray*)curveSlots{
NSMutableArray*slots=[NSMutableArray array];NSInteger n=_channel==0?4:2;
for(NSInteger i=0;i<n;i++)[slots addObject:@{@"xKey":_channel==0?[NSString stringWithFormat:@"masterX%ld",(long)i]:[NSString stringWithFormat:@"channel%ldX%ld",(long)_channel-1,(long)i],@"yKey":[NSString stringWithFormat:@"curveY%ld",(long)(_channel==0?i:4+(_channel-1)*2+i)]}];
for(NSInteger i=0;i<4;i++)[slots addObject:@{@"xKey":[NSString stringWithFormat:@"curve%ldExtraX%ld",(long)_channel,(long)i],@"yKey":[NSString stringWithFormat:@"curve%ldExtra%ld",(long)_channel,(long)i]}];return slots;
}
- (NSArray*)curvePoints{
NSMutableArray*points=[NSMutableArray array];for(NSDictionary*slot in [self curveSlots]){double x=[self graphicValue:slot[@"xKey"]];if(x<0)continue;[points addObject:@{@"xKey":slot[@"xKey"],@"yKey":slot[@"yKey"],@"x":@(x),@"y":@(MAX(0.,MIN(1.,x+[self graphicValue:slot[@"yKey"]]*.01)))}];}
[points sortUsingComparator:^NSComparisonResult(NSDictionary*a,NSDictionary*b){return [a[@"x"]compare:b[@"x"]];}];return points;
}
- (void)layoutGraphicAt:(CGFloat)y{
if(!_snapshot||![self node]||_panel.size.width<180*_scale||_panel.size.height<180*_scale)return;CGFloat s=_scale,x=_panel.origin.x,w=_panel.size.width;
if([_toolType isEqual:@"curve"]){
for(NSInteger c=0;c<4;c++)[self addArea:NSMakeRect(x+12*s+c*(w-24*s)/4,y,(w-30*s)/4,23*s)part:22010+c kind:@"channel" title:@[@"RGB",@"红",@"绿",@"蓝"][c] descriptor:nil index:c];
NSRect graph=NSMakeRect(x+18*s,y+31*s,w-36*s,MAX(45*s,NSMaxY(_panel)-y-84*s));[self addArea:graph part:22000 kind:@"curvePlot" title:@"" descriptor:nil index:0];
if(_selectedX)[self addArea:NSMakeRect(x+12*s,NSMaxY(_panel)-47*s,90*s,20*s)part:22020 kind:@"deletePoint" title:@"删除选中点" descriptor:nil index:0];
}else{
CGFloat diameter=MAX(20*s,MIN((w-48*s)/2,(NSMaxY(_panel)-y-52*s)/2-20*s));
for(NSInteger i=0;i<4;i++){CGFloat cx=x+w*(i%2?.75:.25),top=y+(i/2)*(diameter+29*s);[self addArea:NSMakeRect(cx-diameter/2,top+20*s,diameter,diameter)part:22100+i kind:@"wheel" title:@[@"阴影",@"中间调",@"高光",@"全局"][i] descriptor:nil index:i];}
}
}
- (NSPoint)curvePosition:(NSDictionary*)p rect:(NSRect)r{return NSMakePoint(r.origin.x+[p[@"x"]doubleValue]*r.size.width,r.origin.y+(1-[p[@"y"]doubleValue])*r.size.height);}
- (void)drawGraphic:(NSDictionary*)area{
CGFloat s=_scale;NSRect r=[area[@"rect"]rectValue];NSColor*ink=DYThemeColor(@"text"),*accent=DYThemeColor(@"accent");
if([area[@"kind"]isEqual:@"wheel"]){
NSInteger index=[area[@"index"]integerValue];[self text:area[@"title"]rect:NSMakeRect(r.origin.x,r.origin.y-19*s,r.size.width,17*s)size:10 color:ink alignment:NSTextAlignmentCenter];
// Cached raster is generated from the same HSV convention as the renderer controls.
static NSImage*wheel=nil;static dispatch_once_t once;dispatch_once(&once,^{NSInteger n=192;NSBitmapImageRep*b=[[[NSBitmapImageRep alloc]initWithBitmapDataPlanes:NULL pixelsWide:n pixelsHigh:n bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:n*4 bitsPerPixel:32]autorelease];
for(NSInteger yy=0;yy<n;yy++)for(NSInteger xx=0;xx<n;xx++){double dx=(xx+.5-n*.5)/(n*.5-1),dy=(n*.5-yy-.5)/(n*.5-1),sat=hypot(dx,dy),h=atan2(dy,dx)/(2*M_PI);if(h<0)h++;NSColor*c=[NSColor colorWithSRGBRed:1 green:1 blue:1 alpha:1];c=[[NSColor colorWithHue:h saturation:MIN(1.,sat) brightness:1 alpha:1]colorUsingColorSpace:NSColorSpace.sRGBColorSpace];NSUInteger at=(yy*n+xx)*4;b.bitmapData[at]=lrint(c.redComponent*255);b.bitmapData[at+1]=lrint(c.greenComponent*255);b.bitmapData[at+2]=lrint(c.blueComponent*255);b.bitmapData[at+3]=sat<=1?230:0;}
wheel=[[NSImage alloc]initWithSize:NSMakeSize(n,n)];[wheel addRepresentation:b];});
[wheel drawInRect:r fromRect:NSZeroRect operation:NSCompositingOperationSourceOver fraction:.8 respectFlipped:YES hints:nil];
double hue=[self graphicValue:[NSString stringWithFormat:@"grade%ld_0",(long)index]],sat=[self graphicValue:[NSString stringWithFormat:@"grade%ld_1",(long)index]],angle=hue*M_PI/180,rad=r.size.width*.5*sat/100;
NSPoint p=NSMakePoint(NSMidX(r)+cos(angle)*rad,NSMidY(r)-sin(angle)*rad);[NSColor.blackColor setStroke];NSBezierPath*knob=[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(p.x-5*s,p.y-5*s,10*s,10*s)];knob.lineWidth=3*s;[knob stroke];[NSColor.whiteColor setStroke];knob.lineWidth=1.4*s;[knob stroke];
return;
}
DYDrawSoftControl(r,4*s,YES,NO,NO);[NSGraphicsContext saveGraphicsState];NSRectClip(r);
[[ink colorWithAlphaComponent:.16]setStroke];NSBezierPath*grid=[NSBezierPath bezierPath];grid.lineWidth=.5*s;
for(int i=0;i<5;i++){CGFloat x=r.origin.x+r.size.width*i/4,y=r.origin.y+r.size.height*i/4;[grid moveToPoint:NSMakePoint(x,NSMinY(r))];[grid lineToPoint:NSMakePoint(x,NSMaxY(r))];[grid moveToPoint:NSMakePoint(NSMinX(r),y)];[grid lineToPoint:NSMakePoint(NSMaxX(r),y)];}[grid stroke];
[[ink colorWithAlphaComponent:.2]setStroke];NSBezierPath*diagonal=[NSBezierPath bezierPath];[diagonal moveToPoint:NSMakePoint(NSMinX(r),NSMaxY(r))];[diagonal lineToPoint:NSMakePoint(NSMaxX(r),NSMinY(r))];[diagonal stroke];
// Identical monotone Hermite interpolation to the existing curve editor.
NSArray*points=[self curvePoints];std::vector<NSPoint> p;p.push_back(NSMakePoint(0,0));for(NSDictionary*point in points)p.push_back(NSMakePoint([point[@"x"]doubleValue],[point[@"y"]doubleValue]));p.push_back(NSMakePoint(1,1));
for(size_t i=1;i<p.size();i++)p[i].x=MAX(p[i].x,p[i-1].x+.0001);p.back().x=1;
std::vector<double>slopes(p.size()-1),m(p.size());for(size_t i=0;i<slopes.size();i++)slopes[i]=(p[i+1].y-p[i].y)/MAX(.0001,p[i+1].x-p[i].x);m.front()=slopes.front();m.back()=slopes.back();for(size_t i=1;i+1<p.size();i++)m[i]=slopes[i-1]*slopes[i]>0?(slopes[i-1]+slopes[i])*.5:0;
for(size_t i=0;i<slopes.size();i++){if(fabs(slopes[i])<.00001)m[i]=m[i+1]=0;else{double a=m[i]/slopes[i],b=m[i+1]/slopes[i],v=hypot(a,b);if(v>3){m[i]=3*slopes[i]*a/v;m[i+1]=3*slopes[i]*b/v;}}}
NSBezierPath*curve=[NSBezierPath bezierPath];curve.lineWidth=1.8*s;size_t segment=0;
for(int sample=0;sample<=256;sample++){double x=sample/256.;while(segment+2<p.size()&&x>p[segment+1].x)segment++;double width=MAX(.0001,p[segment+1].x-p[segment].x),t=MAX(0.,MIN(1.,(x-p[segment].x)/width)),t2=t*t,t3=t2*t;double y=(2*t3-3*t2+1)*p[segment].y+(t3-2*t2+t)*width*m[segment]+(-2*t3+3*t2)*p[segment+1].y+(t3-t2)*width*m[segment+1];NSPoint q=NSMakePoint(r.origin.x+x*r.size.width,r.origin.y+(1-MAX(0.,MIN(1.,y)))*r.size.height);if(sample==0)[curve moveToPoint:q];else[curve lineToPoint:q];}
NSColor*color=_channel==0?accent:_channel==1?NSColor.systemRedColor:_channel==2?NSColor.systemGreenColor:NSColor.systemBlueColor;[color setStroke];[curve stroke];
for(NSDictionary*point in points){NSPoint q=[self curvePosition:point rect:r];BOOL selected=[point[@"xKey"]isEqual:_selectedX];[color setFill];NSBezierPath*dot=[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(q.x-4*s,q.y-4*s,8*s,8*s)];[dot fill];[(selected?NSColor.whiteColor:ink)setStroke];dot.lineWidth=(selected?2:1)*s;[dot stroke];}
[NSGraphicsContext restoreGraphicsState];[self text:@"0 输入 → 1" rect:NSMakeRect(r.origin.x,NSMaxY(r)+2*s,r.size.width,14*s)size:8 color:ink alignment:NSTextAlignmentCenter];
}
- (void)beginGraphicAt:(NSPoint)p{
if(!_snapshot||![self node])return;NSMutableDictionary*c=[[_captured mutableCopy]autorelease];NSRect r=[c[@"rect"]rectValue];NSString*xKey=nil,*yKey=nil;
if([c[@"kind"]isEqual:@"curvePlot"]){NSDictionary*nearest=nil;double distance=12*_scale;for(NSDictionary*point in [self curvePoints]){NSPoint q=[self curvePosition:point rect:r];double d=hypot(q.x-p.x,q.y-p.y);if(d<distance){distance=d;nearest=point;}}
if(!nearest)for(NSDictionary*slot in [self curveSlots])if([self graphicValue:slot[@"xKey"]]<0){nearest=slot;break;}
if(!nearest)return;xKey=nearest[@"xKey"];yKey=nearest[@"yKey"];[_selectedX release];_selectedX=[xKey copy];c[@"newPoint"]=@([self graphicValue:xKey]<0);
}else{if(hypot(p.x-NSMidX(r),p.y-NSMidY(r))>r.size.width*.5)return;NSInteger i=[c[@"index"]integerValue];xKey=[NSString stringWithFormat:@"grade%ld_0",(long)i];yKey=[NSString stringWithFormat:@"grade%ld_1",(long)i];}
NSDictionary*xd=[self descriptorForKey:xKey],*yd=[self descriptorForKey:yKey];if(!xd||!yd)return;c[@"xKey"]=xKey;c[@"yKey"]=yKey;
[_captured release];_captured=[c copy];NSDictionary*values=@{xKey:@([self graphicValue:xKey]),yKey:@([self graphicValue:yKey])};[_bridge beginParameters:@[xd,yd]node:_nodeID values:values atTime:_time];
if([c[@"newPoint"]boolValue]||[c[@"kind"]isEqual:@"wheel"])[self updateGraphicAt:p];
}
- (void)updateGraphicAt:(NSPoint)p{
NSString*xKey=_captured[@"xKey"],*yKey=_captured[@"yKey"];if(!xKey||!yKey)return;NSRect r=[_captured[@"rect"]rectValue];double x,y;
if([_captured[@"kind"]isEqual:@"curvePlot"]){x=MAX(.001,MIN(.999,(p.x-r.origin.x)/MAX(1.,r.size.width)));double output=MAX(0.,MIN(1.,1-(p.y-r.origin.y)/MAX(1.,r.size.height)));
for(NSDictionary*point in [self curvePoints])if(![point[@"xKey"]isEqual:xKey]){double other=[point[@"x"]doubleValue];if(fabs(x-other)<.001)x=x<other?other-.001:other+.001;}x=MAX(.001,MIN(.999,x));y=(output-x)*100;
}else{double dx=p.x-NSMidX(r),dy=NSMidY(r)-p.y;x=atan2(dy,dx)*180/M_PI;if(x<0)x+=360;y=MIN(100.,hypot(dx,dy)/(r.size.width*.5)*100);}
NSDictionary*values=@{xKey:@(x),yKey:@(y)};[_draftValues addEntriesFromDictionary:values];[_bridge changeValues:values];
}
- (void)deleteSelectedPoint{
if(!_selectedX)return;for(NSDictionary*point in [self curvePoints])if([point[@"xKey"]isEqual:_selectedX]){NSString*x=point[@"xKey"],*y=point[@"yKey"];[_bridge beginParameters:@[[self descriptorForKey:x],[self descriptorForKey:y]]node:_nodeID values:@{x:@([self graphicValue:x]),y:@([self graphicValue:y])}atTime:_time];[_bridge changeValues:@{x:@(-1),y:@0}];[_bridge endGesture];break;}[_selectedX release];_selectedX=nil;
}