#pragma once #import #include "DYScopeMath.hpp" #include "SpiderCore/DYColorMath.h" // Shared Cb/Cr display coordinates. Positive Cr goes up in a flipped view. static inline NSPoint DYScopePoint(NSRect rect, double cb, double cr) { return NSMakePoint(NSMidX(rect)+cb*NSWidth(rect)/(2*dyscope::ChromaLimit), NSMidY(rect)-cr*NSHeight(rect)/(2*dyscope::ChromaLimit)); } static inline NSPoint DYSkinScopePoint(NSRect rect, double radiusFraction) { return DYScopePoint(rect,dyscope::SkinToneCb*dyscope::ChromaLimit*radiusFraction, dyscope::SkinToneCr*dyscope::ChromaLimit*radiusFraction); } // Stored spider points remain HSV. Only the displayed angle changes; the // distance from center still represents the mesh's editable saturation ring. static inline float DYSpiderHueAtChroma(float cb,float cr) { auto p=dyscope::chromaRGB({cb,cr}); return dyRGBHSV(float3{p.r,p.g,p.b}).x; } static inline NSPoint DYSpiderPlotPoint(NSRect rect,double x,double y) { double distance=hypot(x-.5,y-.5); if(distance==0)return NSMakePoint(NSMidX(rect),NSMidY(rect)); double hue=atan2(y-.5,x-.5)/(2*M_PI); auto rgb=dyHSVRGB(float3{(float)hue,1,1}); dyscope::RGBA color={rgb.x,rgb.y,rgb.z,1}; auto c=dyscope::chroma(color,dyscope::luminance(color)); double scale=distance*2*dyscope::ChromaLimit/hypot(c.cb,c.cr); return DYScopePoint(rect,c.cb*scale,c.cr*scale); } static inline NSPoint DYSpiderMeshPoint(NSRect rect,NSPoint point) { if(NSIsEmptyRect(rect))return NSMakePoint(.5,.5); double cb=(point.x-NSMidX(rect))/NSWidth(rect),cr=(NSMidY(rect)-point.y)/NSHeight(rect); double radius=hypot(cb,cr),angle=2*M_PI*DYSpiderHueAtChroma(cb,cr); return NSMakePoint(.5+cos(angle)*radius,.5+sin(angle)*radius); } static inline NSPoint DYSpiderMeshDelta(NSRect rect,double x,double y,NSPoint movement) { if(NSIsEmptyRect(rect)||NSEqualPoints(movement,NSZeroPoint))return NSZeroPoint; NSPoint target=DYSpiderPlotPoint(rect,x,y); target.x+=movement.x;target.y+=movement.y; NSPoint mapped=DYSpiderMeshPoint(rect,target); return NSMakePoint(mapped.x-x,mapped.y-y); } static inline void DYDrawSpiderWheel(NSRect rect) { // Cache the smooth color field. A neutral center separates the trace from // the hue around the perimeter, and the field is shared by every mesh size. static NSImage *wheel; static dispatch_once_t once; dispatch_once(&once,^{ constexpr NSInteger side=256; NSBitmapImageRep *bitmap=[[[NSBitmapImageRep alloc]initWithBitmapDataPlanes:nil pixelsWide:side pixelsHigh:side bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:side*4 bitsPerPixel:32]autorelease]; for(NSInteger y=0;y=72*scale){ NSDictionary *attrs=@{NSFontAttributeName:[NSFont systemFontOfSize:9*scale weight:NSFontWeightMedium], NSForegroundColorAttributeName:[NSColor colorWithSRGBRed:1 green:.88 blue:.74 alpha:1]}; NSString *label=@"肤色";NSSize size=[label sizeWithAttributes:attrs]; NSPoint anchor=DYSkinScopePoint(rect,.83); anchor.x+=4*scale;anchor.y-=size.height*.65; NSRect tag=NSInsetRect(NSMakeRect(anchor.x,anchor.y,size.width,size.height),-3*scale,-1*scale); [[NSColor colorWithWhite:.09 alpha:.93]setFill]; [[NSBezierPath bezierPathWithRoundedRect:tag xRadius:3*scale yRadius:3*scale]fill]; [label drawAtPoint:anchor withAttributes:attrs]; } [NSGraphicsContext restoreGraphicsState]; } static inline void DYDrawSpiderNet(NSBezierPath *net,CGFloat scale) { net.lineWidth=1.65*scale; [[NSColor colorWithWhite:0 alpha:.63]setStroke];[net stroke]; net.lineWidth=.7*scale; [[NSColor colorWithWhite:1 alpha:.72]setStroke];[net stroke]; } static inline void DYDrawSpiderNode(NSPoint p,CGFloat scale,NSColor *selection,BOOL pinned) { CGFloat radius=(selection?4.2:2.9)*scale; NSBezierPath *dot=[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(p.x-radius,p.y-radius,2*radius,2*radius)]; [(selection?:[NSColor colorWithWhite:.62 alpha:1])setFill];[dot fill]; dot.lineWidth=(selection?1.9:1.55)*scale; [[NSColor colorWithWhite:.065 alpha:1]setStroke];[dot stroke]; if(pinned){ [NSColor.blackColor setFill]; NSRectFill(NSMakeRect(p.x-scale,p.y-scale,2*scale,2*scale)); } }