在xcode中制作RGB颜色 [英] Making RGB color in xcode

查看:313
本文介绍了在xcode中制作RGB颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用照相馆颜色的RGB值,并在xcode中使用相同值。颜色-R-160,G-97,B-5 ...... photoshop中的颜色显示为黄色,但在xcode中我用了

I am using RGB values of a color from photo shop 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

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

颜色显得偏白。

为什么这种差异正在发生?

Why this difference is happening?

推荐答案

Objective-C



你必须给出两者之间的值0和1.0。因此,将RGB值除以255.

Objective-C

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

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

更新:

你也可以使用这个宏

#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



这是正常颜色synax

Swift

This is the normal color 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对宏不太友好

Swift is not much friendly with macros


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

所以我们使用扩展名

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)
    }
}

您可以像

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

这篇关于在xcode中制作RGB颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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