有没有传统的方法来反转NSColor值? [英] Is there a conventional method for inverting NSColor values?

查看:478
本文介绍了有没有传统的方法来反转NSColor值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种方法来反转任意更简单,它产生更好的结果:

  return [NSColor colorWithCalibratedRed:(1.0  -  [original redComponent])
green:(1.0 - [original greenComponent])
blue:(1.0 - [original blueComponent])
alpha: ]];

解决方案

简单:组件都是0-1,

例如:




  • Red = 1 ,0,0; complement = 0,1,1 = cyan

  • Yellow = 1,1,0; complement = 0,0,1 = blue

  • White = 1,1,1; complement = 0,0,0 = black

  • 深橙色= 1,0.25,0; complement = 0,0.75,1 = sky blue



正如你可以猜到的,这是一个对称运算。反转反转会让你准确地开始的颜色。


I'm looking for a way to invert arbitrary NSColor values at runtime, and there doesn't appear to be any built-in method to do so.

I will be using a category to extend NSColor as follows:

NSColor * invertedColor = [someOtherColor inverted];

Here is the category method prototype that I am using:

@implementation NSColor (MyCategories)
- (NSColor *)inverted
{
    NSColor * original = [self colorUsingColorSpaceName:
                      NSCalibratedRGBColorSpace];
    return ...?
}
@end

Can anyone fill in the blanks? This doesn't have to be perfect, but I would like it to make some sense. i.e.:

  1. [[[NSColor someColor] inverted] inverted] would result in a color very close to the original color

  2. [[NSColor whiteColor] inverted] would be very close to [NSColor blackColor]

  3. Inverted colors would be on opposite sides of the color wheel. (red & green, yellow & violet, etc.)

The alpha value should remain the same as the original NSColor. I'm only looking to invert the color, not the transparency.

UPDATE 3: (now in color!)

It turns out that using a complementary color (a color with a hue 180° away from the original hue) is not quite enough, since white and black don't really have a hue value. Starting from the answer provided by phoebus, this is what I came up with:

CGFloat hue = [original hueComponent];
if (hue >= 0.5) { hue -= 0.5; } else { hue += 0.5; }
return [NSColor colorWithCalibratedHue:hue
                            saturation:[original saturationComponent]
                            brightness:(1.0 - [original brightnessComponent])
                                 alpha:[original alphaComponent]];

The hue is still rotated 180°, but I also invert the brightness component so that very dark colors become very bright (and vice versa). This takes care of the black and white cases, but it inverts just about every color to black (which violates my double-inversion rule). Here are the results:

Now, Peter Hosey's approach is much simpler, and it produces better results:

return [NSColor colorWithCalibratedRed:(1.0 - [original redComponent])
                                 green:(1.0 - [original greenComponent])
                                  blue:(1.0 - [original blueComponent])
                                 alpha:[original alphaComponent]];

解决方案

Simple: The components are all 0–1, so subtract each component from 1 to get the complement's value.

Examples:

  • Red = 1, 0, 0; complement = 0, 1, 1 = cyan
  • Yellow = 1, 1, 0; complement = 0, 0, 1 = blue
  • White = 1, 1, 1; complement = 0, 0, 0 = black
  • Deep orange = 1, 0.25, 0; complement = 0, 0.75, 1 = sky blue

As you can guess, this is a symmetric operation. Inverting the inverse will get you exactly the color you started with.

这篇关于有没有传统的方法来反转NSColor值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆