Search code examples
cocoansviewnscolor

sRGB to NSColor


I am trying to draw an inner shadow in an NSView. The shadow itself is not the problem, but the color setting is driving me nuts :/

#define ShadowBlurRadius 10.0
#define SRGB (CGFloat [4]){184.0, 184.0, 184.0, 1.0}

@implementation SWShadowedView

- (void)drawRect:(NSRect)dirtyRect {

NSGraphicsContext *context = [NSGraphicsContext currentContext];
[context saveGraphicsState];

[context setCompositingOperation:NSCompositePlusDarker];


NSBezierPath *path = [NSBezierPath bezierPathWithRect:NSMakeRect(0, dirtyRect.size.height -ShadowBlurRadius, self.superview.frame.size.width, ShadowBlurRadius)];

[[NSColor whiteColor] setStroke];

NSShadow * shadow = [[NSShadow alloc] init];

NSColorSpace *colorSpace = [NSColorSpace sRGBColorSpace];

NSColor *color = [NSColor colorWithColorSpace:colorSpace components:SRGB count:4];

[shadow setShadowColor:color];

[shadow setShadowBlurRadius:ShadowBlurRadius];
[shadow set];

[path stroke];

[context restoreGraphicsState];

[super drawRect:dirtyRect];
}

@end

If I replace the shadow color with [NSColor redColor] it works but with the wrong color. This is where I got the sRGB from: link

The way to convert sRGB to NSColor is taken from another post from here but obviously it's not working.

best regards


Solution

  • Use RGB not sRGB:

    You can create color with RGB like this:

    float red = 182.0f/256.0f;
    float green = 182.0f/256.0f;
    float blue = 182.0f/256.0f;
    
    
    NSColor *color = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:1.0f];