134 行
6.3 KiB
Objective-C
134 行
6.3 KiB
Objective-C
#pragma once
|
|
#import <AppKit/AppKit.h>
|
|
#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<side;y++)for(NSInteger x=0;x<side;x++){
|
|
float cb=(x+.5f-side*.5f)/(side*.5f),cr=(side*.5f-y-.5f)/(side*.5f);
|
|
float radius=std::min(1.f,std::hypot(cb,cr));
|
|
auto color=dyHSVRGB(float3{DYSpiderHueAtChroma(cb,cr),radius*.93f,.225f+.17f*radius});
|
|
uint8_t *out=bitmap.bitmapData+4*(y*side+x);
|
|
out[0]=std::lround(color.x*255);out[1]=std::lround(color.y*255);
|
|
out[2]=std::lround(color.z*255);out[3]=255;
|
|
}
|
|
wheel=[[NSImage alloc]initWithSize:NSMakeSize(side,side)];
|
|
[wheel addRepresentation:bitmap];
|
|
});
|
|
[wheel drawInRect:rect fromRect:NSZeroRect operation:NSCompositingOperationSourceOver
|
|
fraction:1 respectFlipped:YES hints:nil];
|
|
}
|
|
|
|
static inline void DYDrawSpiderTrace(NSRect rect, NSData *raster) {
|
|
constexpr size_t side=dyscope::Bins;
|
|
if(raster.length!=side*side*4)return;
|
|
CGDataProviderRef provider=CGDataProviderCreateWithCFData((CFDataRef)raster);
|
|
CGColorSpaceRef space=CGColorSpaceCreateDeviceRGB();
|
|
CGImageRef image=CGImageCreate(side,side,8,32,side*4,space,kCGImageAlphaPremultipliedLast,
|
|
provider,nullptr,false,kCGRenderingIntentDefault);
|
|
if(image){
|
|
NSImage *trace=[[[NSImage alloc]initWithCGImage:image size:NSZeroSize]autorelease];
|
|
[trace drawInRect:rect fromRect:NSZeroRect operation:NSCompositingOperationSourceOver
|
|
fraction:1 respectFlipped:YES hints:@{NSImageHintInterpolation:@(NSImageInterpolationLow)}];
|
|
CGImageRelease(image);
|
|
}
|
|
CGColorSpaceRelease(space);CGDataProviderRelease(provider);
|
|
}
|
|
|
|
static inline void DYDrawSpiderScope(NSRect rect,NSData *raster,CGFloat scale) {
|
|
if(NSIsEmptyRect(rect))return;
|
|
[NSGraphicsContext saveGraphicsState];
|
|
[[NSBezierPath bezierPathWithOvalInRect:rect]addClip];
|
|
DYDrawSpiderWheel(rect);
|
|
DYDrawSpiderTrace(rect,raster);
|
|
|
|
NSBezierPath *line=[NSBezierPath bezierPath];
|
|
[line moveToPoint:DYSkinScopePoint(rect,.08)];
|
|
[line lineToPoint:DYSkinScopePoint(rect,.93)];
|
|
CGFloat dashes[]={4*scale,3*scale};
|
|
[line setLineDash:dashes count:2 phase:0];
|
|
line.lineWidth=2.7*scale;
|
|
[[NSColor colorWithWhite:0 alpha:.9]setStroke];[line stroke];
|
|
line.lineWidth=1.15*scale;
|
|
[[NSColor colorWithSRGBRed:1 green:.79 blue:.57 alpha:1]setStroke];[line stroke];
|
|
if(NSWidth(rect)>=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));
|
|
}
|
|
}
|