Re: NSColor contrasting color?
Re: NSColor contrasting color?
- Subject: Re: NSColor contrasting color?
- From: Martin Hewitson <email@hidden>
- Date: Sat, 15 Sep 2012 14:04:03 +0200
Works splendidly, thank you! I just check if my color is darker than 0.7 and use highlightWithLevel otherwise use shadowWithLevel.
Martin
On 15, Sep, 2012, at 11:14 AM, Markus Spoettl <email@hidden> wrote:
> On 9/15/12 10:52 AM, Martin Hewitson wrote:
>> I'm not sure what the correct terminology is here. I have a text editor app
>> and the user can set the background color of the editor. The user can also
>> set the line length for wrapping. Past that length, I have been drawing a
>> shaded rect (in the text view subclass' -drawViewBackgroundInRect:). For the
>> shaded rect, I've been using NSColor's shadowWithLevel:. This works fine for
>> most colors, but obviously not for a black background. I suppose in some
>> cases I could highlightWithLevel and in others shadowWithLevel, but I don't
>> know how to distinguish between these cases. Is there something else I could
>> do to get a reasonable contrasting color?
>
> You can create a monoChrome color from your background color and ask for its -whiteComponent which will tell you how light it is. I use a category on NSColor like this:
>
> @implementation NSColor (lightness)
>
> - (BOOL)isDarkerThan:(float)lightness
> {
> NSColor *monoColor = [self colorUsingColorSpaceName:@"NSCalibratedWhiteColorSpace"];
> return ([monoColor whiteComponent] < lightness);
> }
>
> @end
>
> You can then query your color using a value between 0.0 and 1.0 and choose the appropriate color for your shadow.
>
> Regards
> Markus
> --
> __________________________________________
> Markus Spoettl
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden