ios 在 Xcode 中制作 RGB 颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10379009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:09:53  来源:igfitidea点击:

Making RGB color in Xcode

iosobjective-cuicolor

提问by amar

I am using RGB values of a color from Photoshop and using the same in Xcode the values are.Color-R-160,G-97,B-5...the color in Photoshop appears yellowish but in Xcode when I used

我正在使用来自 Photoshop 的颜色的 RGB 值,并在 Xcode 中使用相同的值。Color-R-160,G-97,B-5 ... Photoshop 中的颜色看起来偏黄,但在 Xcode 中使用时

myLabel.textColor = [UIColor colorWithRed:160 green:97 blue:5 alpha:1] ;

the color appears whitish.

颜色显得发白。

Why this difference is happening?

为什么会出现这种差异?

回答by iPrabu

Objective-C

目标-C

You have to give the values between 0 and 1.0. So divide the RGB values by 255.

您必须给出介于 0 和 1.0 之间的值。因此,将 RGB 值除以 255。

myLabel.textColor= [UIColor colorWithRed:(160/255.0) green:(97/255.0) blue:(5/255.0) alpha:1] ;

Update:

更新:

You can also use this macro

你也可以使用这个宏

#define Rgb2UIColor(r, g, b)  [UIColor colorWithRed:((r) / 255.0) green:((g) / 255.0) blue:((b) / 255.0) alpha:1.0]

and you can call in any of your class like this

你可以像这样打电话给你的任何班级

 myLabel.textColor = Rgb2UIColor(160, 97, 5);

Swift

迅速

This is the normal color synax

这是正常的颜色synax

myLabel.textColor = UIColor(red: (160/255.0), green: (97/255.0), blue: (5/255.0), alpha: 1.0) 
//The values should be between 0 to 1

Swift is not much friendly with macros

Swift 对宏不太友好

Complex macros are used in C and Objective-C but have no counterpart in Swift. Complex macros are macros that do not define constants, including parenthesized, function-like macros. You use complex macros in C and Objective-C to avoid type-checking constraints or to avoid retyping large amounts of boilerplate code. However, macros can make debugging and refactoring difficult. In Swift, you can use functions and generics to achieve the same results without any compromises. Therefore, the complex macros that are in C and Objective-C source files are not made available to your Swift code.

复杂的宏在 C 和 Objective-C 中使用,但在 Swift 中没有对应的宏。复杂宏是不定义常量的宏,包括带括号的、类似函数的宏。您在 C 和 Objective-C 中使用复杂的宏来避免类型检查约束或避免重新输入大量样板代码。但是,宏会使调试和重构变得困难。在 Swift 中,您可以使用函数和泛型来实现相同的结果,而不会做出任何妥协。因此,您的 Swift 代码无法使用 C 和 Objective-C 源文件中的复杂宏。

So we use extension for this

所以我们为此使用扩展名

extension UIColor {
    convenience init(_ r: Double,_ g: Double,_ b: Double,_ a: Double) {
        self.init(red: r/255, green: g/255, blue: b/255, alpha: a)
    }
}

You can use it like

你可以像这样使用它

myLabel.textColor = UIColor(160.0, 97.0, 5.0, 1.0)

回答by jweyrich

You already got the right answer, but if you dislike the UIColorinterface like me, you can do this:

你已经得到了正确的答案,但如果你UIColor像我一样不喜欢这个界面,你可以这样做:

#import "UIColor+Helper.h"
// ...
myLabel.textColor = [UIColor colorWithRGBA:0xA06105FF];

UIColor+Helper.h:

UIColor+Helper.h:

#import <UIKit/UIKit.h>

@interface UIColor (Helper)
+ (UIColor *)colorWithRGBA:(NSUInteger)color;
@end

UIColor+Helper.m:

UIColor+Helper.m:

#import "UIColor+Helper.h"

@implementation UIColor (Helper)

+ (UIColor *)colorWithRGBA:(NSUInteger)color
{
    return [UIColor colorWithRed:((color >> 24) & 0xFF) / 255.0f
                           green:((color >> 16) & 0xFF) / 255.0f
                            blue:((color >> 8) & 0xFF) / 255.0f
                           alpha:((color) & 0xFF) / 255.0f];
}

@end

回答by Allamaprabhu

Yeah.ios supports RGB valur to range between 0 and 1 only..its close Range [0,1]

是的。ios 只支持 RGB 值的范围在 0 和 1 之间..它的接近范围 [0,1]

回答by hfossli

Color picker plugin for Interface Builder

Interface Builder的颜色选择器插件

There's a nice color picker from Panic which works well with IB: http://panic.com/~wade/picker/

Panic 有一个很好的颜色选择器,可以很好地与 IB 配合使用:http: //panic.com/~wade/picker/

Xcode plugin

Xcode插件

This one gives you a GUI for choosing colors: http://www.youtube.com/watch?v=eblRfDQM0Go

这个为您提供了一个用于选择颜色的 GUI:http: //www.youtube.com/watch?v=eblRfDQM0Go

Objective-C

目标-C

UIColor *color = [UIColor colorWithRed:(160/255.0) green:(97/255.0) blue:(5/255.0) alpha:1.0];

Swift

迅速

let color = UIColor(red: 160/255, green: 97/255, blue: 5/255, alpha: 1.0)

Pods and libraries

Pod 和库

There's a nice pod named MPColorTools: https://github.com/marzapower/MPColorTools

有一个不错的 pod 名为MPColorToolshttps: //github.com/marzapower/MPColorTools

回答by hfossli

The values are determined by the bit of the image. 8 bit 0 to 255

这些值由图像的位决定。8 位 0 到 255

16 bit...some ridiculous number..0 to 65,000 approx.

16 位...一些荒谬的数字...大约 0 到 65,000。

32 bit are 0 to 1

32 位是 0 到 1

I use .004 with 32 bit images...this gives 1.02 as a result when multiplied by 255

我将 .004 与 32 位图像一起使用...乘以 255 时结果为 1.02