programing

xcode에서 RGB 색상 만들기

nasanasas 2020. 11. 22. 19:50
반응형

xcode에서 RGB 색상 만들기


포토샵에서 색상의 RGB 값을 사용하고 있으며 xcode에서 동일한 값을 사용하고 있습니다 .Color-R-160, G-97, B-5 ​​... 포토샵의 색상은 노란색으로 보이지만 사용할 때는 xcode에서

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

색상이 하얗게 나타납니다.

왜 이런 차이가 발생합니까?


목표 -C

0에서 1.0 사이의 값을 제공해야합니다. 따라서 RGB 값을 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]

이런 식으로 어떤 수업에서든 전화를 걸 수 있습니다.

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

빠른

이것은 정상적인 색상 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는 매크로에 그다지 친숙하지 않습니다.

복잡한 매크로는 C와 Objective-C에서 사용되지만 Swift에는 대응되지 않습니다. 복잡한 매크로는 괄호로 묶인 함수형 매크로를 포함하여 상수를 정의하지 않는 매크로입니다. C 및 Objective-C에서 복잡한 매크로를 사용하여 유형 검사 제약 조건을 피하거나 많은 양의 상용구 코드를 다시 입력하지 않도록합니다. 그러나 매크로는 디버깅 및 리팩토링을 어렵게 만들 수 있습니다. Swift에서 함수와 제네릭을 사용하여 타협없이 동일한 결과를 얻을 수 있습니다. 따라서 C 및 Objective-C 소스 파일에있는 복잡한 매크로는 Swift 코드에서 사용할 수 없습니다.

그래서 우리는 이것을 위해 확장을 사용합니다.

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)

이미 정답을 얻었지만 UIColor저와 같은 인터페이스가 마음에 들지 않으면 다음 과 같이 할 수 있습니다.

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

UIColor + Helper.h :

#import <UIKit/UIKit.h>

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

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

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


Color picker plugin for Interface Builder

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

Xcode plugin

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

Code

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

Pods and libraries

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


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

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

32 bit are 0 to 1

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

참고URL : https://stackoverflow.com/questions/10379009/making-rgb-color-in-xcode

반응형