問題描述
我正在為 iOS 創建一個顏色選擇器.我想讓用戶選擇亮度(亮度)并讓色輪反映這種變化.我正在使用 Core Image 通過 CIColorControls 過濾器修改亮度.這是我的代碼:
I'm creating a color picker for iOS. I would like to enable the user to select the brightness (luminance) and have the color wheel reflect this change. I'm using Core Image to modify the brightness with the CIColorControls filter. Here's my code:
-(CIImage *)oldPhoto:(CIImage *)img withBrightness:(float)intensity
{
CIFilter *lighten = [CIFilter filterWithName:@"CIColorControls"];
[lighten setValue:img forKey:kCIInputImageKey];
[lighten setValue:@((intensity * 2.0) - 1.0) forKey:@"inputBrightness"];
return lighten.outputImage;
}
以下是強度 = 0.5 (inputBrightness = 0) 時色輪的外觀:
Here's how the color wheel looks with intensity = 0.5 (inputBrightness = 0):
問題是當強度
時色輪看起來不對.0.5.例如,以下是強度 = 0.3 (inputBrightness = -0.4) 時的樣子:
The problem is that the color wheel looks wrong when intensity < 0.5. For example, here's how it looks with intensity = 0.3 (inputBrightness = -0.4):
請注意,中間有一個黑色圓圈,圖像的其余部分也沒有正確變暗.這應該是一個HSL色輪,所以我想我真正想要改變的是亮度,而不是亮度.
Notice that there's a black circle in the middle, and the rest of the image hasn't been darkened correctly either. This is supposed to be an HSL color wheel, so I guess that what I actually want to change is the luminance, not the brightness.
首先,誰能解釋一下為什么圖片看起來像這樣?我不是色彩專家.奇怪的是,圓的中心很快就變黑了,而它的邊緣卻沒有變暗很多.
First, can anyone explain why the image looks like this? I'm not an expert on color; it seems odd that the center of the circle quickly clips to black while the edges of it don't darken much.
二、怎樣才能達到我想要的效果?
Second, how can I achieve the effect I want?
這就是我真正想要的圖像外觀:
Here's how I actually WANT the image to look:
這是使用自定義 HSL 函數和亮度 = 0.3 創建的.這在 CPU 上運行,所以它對我的需要來說太慢了.我很樂意發布此 HSL 函數的代碼,但我沒有包含它,因為它似乎沒有立即相關.想看就問吧.
This was created with a custom HSL function and luminance = 0.3. This runs on the CPU, so it's far too slow for my needs. I'd be happy to post the code for this HSL function, but I didn't include it because it didn't seem immediately relevant. If you want to see it, just ask.
如果您有任何問題,或者有任何不清楚的地方,請告訴我.謝謝!
Please let me know if you have any questions, or if anything seems unclear. Thanks!
推薦答案
我還發現CIColorControls
的kCIInputBrightnessKey
的非線性很煩人.我采用了線性 CIToneCurve
:
I also found the non-linearity of the kCIInputBrightnessKey
of CIColorControls
to be annoying. I employed a linear CIToneCurve
:
/** Change luminosity of `CIImage`
@param inputImage The `CIImage` of the image to have it's luminosity changed.
@param luminosity The percent change of the luminosity, ranging from -1.0 to 1.0.
@return `CIImage` of image with luminosity changed. If luminosity of 0.0 used, original `inputImage` is returned.
*/
- (CIImage *)changeLuminosityOfCIImage:(CIImage *)inputImage luminosity:(CGFloat)luminosity
{
if (luminosity == 0)
return inputImage;
NSParameterAssert(luminosity >= -1.0 && luminosity <= 1.0);
CIFilter *toneCurveFilter = [CIFilter filterWithName:@"CIToneCurve"];
[toneCurveFilter setDefaults];
[toneCurveFilter setValue:inputImage forKey:kCIInputImageKey];
if (luminosity > 0)
{
[toneCurveFilter setValue:[CIVector vectorWithX:0.0 Y:luminosity] forKey:@"inputPoint0"];
[toneCurveFilter setValue:[CIVector vectorWithX:0.25 Y:luminosity + 0.25 * (1 - luminosity)] forKey:@"inputPoint1"];
[toneCurveFilter setValue:[CIVector vectorWithX:0.50 Y:luminosity + 0.50 * (1 - luminosity)] forKey:@"inputPoint2"];
[toneCurveFilter setValue:[CIVector vectorWithX:0.75 Y:luminosity + 0.75 * (1 - luminosity)] forKey:@"inputPoint3"];
[toneCurveFilter setValue:[CIVector vectorWithX:1.0 Y:1.0] forKey:@"inputPoint4"];
}
else
{
[toneCurveFilter setValue:[CIVector vectorWithX:0.0 Y:0.0] forKey:@"inputPoint0"];
[toneCurveFilter setValue:[CIVector vectorWithX:0.25 Y:0.25 * (1 + luminosity)] forKey:@"inputPoint1"];
[toneCurveFilter setValue:[CIVector vectorWithX:0.50 Y:0.50 * (1 + luminosity)] forKey:@"inputPoint2"];
[toneCurveFilter setValue:[CIVector vectorWithX:0.75 Y:0.75 * (1 + luminosity)] forKey:@"inputPoint3"];
[toneCurveFilter setValue:[CIVector vectorWithX:1.0 Y:1 + luminosity] forKey:@"inputPoint4"];
}
return [toneCurveFilter outputImage];
}
這是您的圖像,使用上述例程將亮度降低 30%:
Here is your image, reducing the luminosity by 30% using the above routine:
可以使用 CIToneCurve
來完成.無論它是否比您的例行程序更快,您都會對其進行基準測試.
It can be done with CIToneCurve
. Whether it's faster than your routine, you'll have benchmark it.
這篇關于Core Image CIColorControls 亮度濾鏡會產生錯誤的效果.如何更改圖像的亮度?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!