2012-04-09 59 views
12

我可以設置的UIColor與RGB值:是否有將UIColor轉換爲色相飽和度亮度的功能?

[UIColor colorWithRed:0.53 green:0.37 blue:0.11 alpha:1.00]; 

我可以設置的UIColor與HSB值:

[UIColor colorWithHue:0.10 saturation:0.16 brightness:0.13 alpha:1.00]; 

我還可以將其轉換回RGB:

CGFloat* colors = CGColorGetComponents(Color1.CGColor); 

但如何我可以從色彩中獲得HSB?

+1

http://stackoverflow.com/questions/9142427/uicolor-conversion-from-rgb-to-hsv-set-brightness-to-uicolor http://stackoverflow.com/的可能的複製問題/ 3017553 /轉換從rgb到hsl -with-objective-c http:// stackoverflow。com/questions/1050874/how-do-i-convert-rgb-into-hsv-in-cocoa-touch – 2012-04-09 10:14:24

回答

40

使用UIColor方法: getHue:saturation:brightness:alpha:

從Apple文檔:「返回彌補了HSB色彩空間的顏色成分」

- (BOOL)getHue:(CGFloat *)hue saturation:(CGFloat *)saturation brightness:(CGFloat *)brightness alpha:(CGFloat *)alpha 

實施例:

UIColor *testColor = [UIColor colorWithRed:0.53 green:0.37 blue:0.11 alpha:1.00]; 

CGFloat hue; 
CGFloat saturation; 
CGFloat brightness; 
CGFloat alpha; 
BOOL success = [testColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]; 
NSLog(@"success: %i hue: %0.2f, saturation: %0.2f, brightness: %0.2f, alpha: %0.2f", success, hue, saturation, brightness, alpha); 

的NSLog輸出:

成功:1色調:0.10,飽和度:0.79,亮度:0.53,α-:1.00

這裏是@WhiteTiger提供的方法的修正版本:

// Test values 
CGFloat red = 0.53; 
CGFloat green = 0.37; 
CGFloat blue = 0.11; 

CGFloat hue = 0; 
CGFloat saturation = 0; 
CGFloat brightness = 0; 

CGFloat minRGB = MIN(red, MIN(green,blue)); 
CGFloat maxRGB = MAX(red, MAX(green,blue)); 

if (minRGB==maxRGB) { 
    hue = 0; 
    saturation = 0; 
    brightness = minRGB; 
} else { 
    CGFloat d = (red==minRGB) ? green-blue : ((blue==minRGB) ? red-green : blue-red); 
    CGFloat h = (red==minRGB) ? 3 : ((blue==minRGB) ? 1 : 5); 
    hue = (h - d/(maxRGB - minRGB))/6.0; 
    saturation = (maxRGB - minRGB)/maxRGB; 
    brightness = maxRGB; 
} 
NSLog(@"hue: %0.2f, saturation: %0.2f, value: %0.2f", hue, saturation, brightness); 

的NSLog輸出:

色調:0.10,飽和度:0.79,值:0.53

+2

但這個功能只能啓動5.0 – WhiteTiger 2012-04-09 11:08:27

+0

現在有超過75%的iOS用戶在5.0以上,購買和/或更新應用程序的用戶。如果<5.0是要求,請更新您的問題。 – zaph 2012-04-09 11:16:33

+0

你知道,但有些情況下,比如有義務保持與以前版本的兼容性。 – WhiteTiger 2012-04-09 11:22:25

4

當心,這是一個草案,但你可以試試這個代碼,如果你是低到5.0版

... 
CGFloat computedH = 0; 
CGFloat computedS = 0; 
CGFloat computedV = 0; 

CGFloat minRGB = MIN(r, MIN(g,b)); 
CGFloat maxRGB = MAX(r, MAX(g,b)); 

if (minRGB==maxRGB) { 
    computedH = 0; 
    computedS = 0; 
    computedV = minRGB; 
} else { 
    double d = (r==minRGB) ? g-b : ((b==minRGB) ? r-g : b-r); 
    double h = (r==minRGB) ? 3 : ((b==minRGB) ? 1 : 5); 
    computedH = (60*(h - d/(maxRGB - minRGB)))/360.; 
    computedS = ((maxRGB - minRGB)/maxRGB); 
    computedV = maxRGB; 
} 
... 
+0

評論:這行'r = r/255。 G =克/ 255 .; b = b/255;如果這些值已經是'CGFloat',則不需要。最後的'computedV'需要被360.0除以獲得0.0到1.0之間的值。 'double'類型可能會更好,因爲'CGFloat'。 – zaph 2012-04-09 11:29:38

+0

我糾正了你的轉換例程,並將其添加到我的答案與測試用例。 – zaph 2012-04-09 11:37:11

+0

感謝您的更正 – WhiteTiger 2012-04-09 11:40:21

17

下面是使用雨燕的功能(擴展,計算性能和元組)做同樣的事情,在短短的一個很好的方式幾行代碼。

extension UIColor { 
    var hsba: (h: CGFloat, s: CGFloat, b: CGFloat, a: CGFloat) { 
    var hsba: (h: CGFloat, s: CGFloat, b: CGFloat, a: CGFloat) = (0, 0, 0, 0) 
    self.getHue(&(hsba.h), saturation: &(hsba.s), brightness: &(hsba.b), alpha: &(hsba.a)) 
    return hsba 
    } 
} 

雨燕3.2/4小的更新

雨燕3.2/4執行新的警告引發與以前的代碼,因爲你修改同一調用內hsba變量多次getHue

同時訪問參數'hsba',但修改需要獨佔訪問;考慮複製到本地變量。

extension UIColor { 
    var hsba:(h: CGFloat, s: CGFloat,b: CGFloat,a: CGFloat) { 
     var h: CGFloat = 0, s: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0 
     self.getHue(&h, saturation: &s, brightness: &b, alpha: &a) 
     return (h: h, s: s, b: b, a: a) 
    } 
} 
+0

用法: self.inputUIImage .getColors({(色) 讓色調= colors.primaryColor.hsba.h self.hueFilter .hue =色調 ?}) – ripegooseberry 2016-04-22 10:30:20

+0

謝謝?將此代碼打包爲元組返回的擴展是非常好的和有用的 – 2017-02-05 19:15:37

+0

您可以舉一個關於如何使用這個(Swift 3.1)的例子嗎? – Tom 2017-03-23 15:09:03

相關問題