I've in a database a few rows where one field is ARGB value for the related color.
I've to read all the rows of these table and convert the ARGB value decimal to a UIColor.
I've googled finding this, but I didn't.
Is there any way to approach this?
Thanks.
Here's the method I came up with to convert an ARGB integer into a UI color. Tested it on several colors we have in our .NET system
+(UIColor *)colorFromARGB:(int)argb {
int blue = argb & 0xff;
int green = argb >> 8 & 0xff;
int red = argb >> 16 & 0xff;
int alpha = argb >> 24 & 0xff;
return [UIColor colorWithRed:red/255.f green:green/255.f blue:blue/255.f alpha:alpha/255.f];
}
text.color = [UIColor colorWithRed:10.0/255.0 green:100.0/255.0 blue:55.0/255.0 alpha:1];
You just need to divide the RGB values by 255 to get things to set up correctly.
you can define a macro and use it throughout your code
#define UIColorFromARGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:((float)((rgbValue & 0xFF000000) >> 24))/255.0]
It's 2021, we're all using Swift ;) So here's a Swift extension to solve this:
extension UIColor {
/* Converts an AARRGGBB into a UIColor like Android Color.parseColor */
convenience init?(hexaARGB: String) {
var chars = Array(hexaARGB.hasPrefix("#") ? hexaARGB.dropFirst() : hexaARGB[...])
switch chars.count {
case 3: chars = chars.flatMap { [$0, $0] }; fallthrough
case 6: chars.append(contentsOf: ["F","F"])
case 8: break
default: return nil
}
self.init(red: .init(strtoul(String(chars[2...3]), nil, 16)) / 255,
green: .init(strtoul(String(chars[4...5]), nil, 16)) / 255,
blue: .init(strtoul(String(chars[6...7]), nil, 16)) / 255,
alpha: .init(strtoul(String(chars[0...1]), nil, 16)) / 255)
}
}
Usage
UIColor(hexaARGB: "#CCFCD204")
Related
I need to assign user profiles colors in a pseudo random consistent way based on their username/name/any string.
How do I do this in objective C iOS 7?
Java based example is here
Compute hex color code for an arbitrary string
There are probably many ways. Here's one:
NSString *someString = ... // some string to "convert" to a color
NSInteger hash = someString.hash;
int red = (hash >> 16) & 0xFF;
int green = (hash >> 8) & 0xFF;
int blue = hash & 0xFF;
UIColor *someColor = [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];
The same string will always give the same color. Different strings will generally give different colors but it is possible that two different strings could give the same color.
My version:
+ (UIColor *)colorForString:(NSString *)string
{
NSUInteger hash = string.hash;
CGFloat hue = ( hash % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( hash % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( hash % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}
It creates more saturated but bright enough and beautiful colors.
I am making an iOS drawing app, and I want to store specific colors in a txt file and then have the file read into an array. So far I have gotten an array to read a file of hex color values, but I do not know how to use the hex values. What I want to be able to do is choose a color by referencing a certain index in the array.
For example
CGContextSetStrokeColorWithColor(context, colors[2]);
I have not found anything that would work for my app.
Its really simple, do something like this
+ (UIColor *)colorWithHexString:(NSString *)hexColorString alpha:(CGFloat)alpha {
unsigned colorValue = 0;
NSScanner *valueScanner = [NSScanner scannerWithString:hexColorString];
if ([hexColorString rangeOfString:#"#"].location != NSNotFound) [valueScanner setScanLocation:1];
[valueScanner scanHexInt:&colorValue];
return [UIColor colorWithRed:((colorValue & 0xFF0000) >> 16)/255.0 green:((colorValue & 0xFF00) >> 8)/255.0 blue:((colorValue & 0xFF) >> 0)/255.0 alpha:alpha];
}
Explanation: let's take a hex color for an example #FFEBCD where the # is an optional. Simply Red: FF Green: EB and Blue: CD so use right-shit operator to filter the positions and pass it to a UIColor factory method.
There is a nice UIColor Extension in github you can try that too. I hope which might be really useful to your App.
i've got my function:
-(void)rgbToHSBWithR:(float)red G:(float)green B:(float)blue {
float brightness = red * 0.3 + green * 0.59 + blue * 0.11; // found in stackoverflow
NSLog(#"%f",brightness);
}
and it isn't work for me.
for example: r:84 g:67 b:73. function return 72.760002. In Photoshop brightness for this color is 33%. What's wrong?
Thanks.
Use UIColor or NSColor:
-(void)rgbToHSBWithR:(float)red G:(float)green B:(float)blue {
// assuming values are in 0 - 1 range, if they are byte representations, divide them by 255
UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1];
float h, s, b;
if ([color getHue:&h saturation:&s brightness:&b alpha:NULL])
{
// h, s, & b now contain what you need
}
}
Your RGB values range from 0 to 255, not 0 to 99 -- you need to divide them first if you want to end up with a percentage:
float brightness = (red / 255.0) * 0.3 + (green / 255.0) * 0.59 + (blue / 255.0) * 0.11;
Also, there is no single conversion between "brightness" and RGB values -- Photoshop may be using a different formula than you are. If you want to know more, I recommend the "Gamma FAQ" and "Color FAQ" by Charles Poynton.
In Objective-C xcode project I have a plist file with which associates integers with hex-color codes. Dynamically I want to use this color-code from plist file and pass that hex value to the following macro to get the UIColor object.
Macro:
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
My actual hex value which I need to pass to this macro is 0xF2A80C, but it is present in the plist file. I can get this as a string. How should I do in this case?
Thanks in advance.
Do you want any details regarding this?.
NSScanner *scanner = [NSScanner scannerWithString:hexString];
unsigned hex;
BOOL success = [scanner scanHexInt:&hex];
UIColor *color = UIColorFromRGB(hex);
What about this:
NSString *textValue = #"0xF2A80C";
long long value = [textValue longLongValue];
UIColor *color = UIColorFromRGB(value);
I've not tested it. So report me if there are problems.
You can use the good old C function strtol with 16 as base.
const char* cString = [myStr cStringUsingEncoding:[NSString defaultCStringEncoding]];
int hexValue = (int)strtol(cString, NULL, 16);
UIColor *color = UIColorFromRGB(hexValue);
I have this macro in my header file:
#define UIColorFromRGB(rgbValue) \
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 \
alpha:1.0]
And I am using this as something like this in my .m file:
cell.textColor = UIColorFromRGB(0x663333);
So I want to ask everyone is this better or should I use this approach:
cell.textColor = [UIColor colorWithRed:66/255.0
green:33/255.0
blue:33/255.0
alpha:1.0];
Which one is the better approach?
or create a separate category, so you only need to import one .h file:
#interface UIColor (util)
+ (UIColor *) colorWithHexString:(NSString *)hex;
+ (UIColor *) colorWithHexValue: (NSInteger) hex;
#end
and
#import "UIColor-util.h"
#implementation UIColor (util)
// Create a color using a string with a webcolor
// ex. [UIColor colorWithHexString:#"#03047F"]
+ (UIColor *) colorWithHexString:(NSString *)hexstr {
NSScanner *scanner;
unsigned int rgbval;
scanner = [NSScanner scannerWithString: hexstr];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:#"#"]];
[scanner scanHexInt: &rgbval];
return [UIColor colorWithHexValue: rgbval];
}
// Create a color using a hex RGB value
// ex. [UIColor colorWithHexValue: 0x03047F]
+ (UIColor *) colorWithHexValue: (NSInteger) rgbValue {
return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
green:((float)((rgbValue & 0xFF00) >> 8))/255.0
blue:((float)(rgbValue & 0xFF))/255.0
alpha:1.0];
}
#end
How about creating your own:
#define RGB(r, g, b) \
[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define RGBA(r, g, b, a) \
[UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
Then use it:
cell.textColor = RGB(0x66, 0x33, 0x33);
Seems simple enough to use, uses hex values for colors and without needing additional calculation overhead.
A middle ground might be your best option. You could define either a regular C or objective-C function to do what your macro is doing now:
// As a C function:
UIColor* UIColorFromRGB(NSInteger rgbValue) {
return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
green:((float)((rgbValue & 0xFF00) >> 8))/255.0
blue:((float)(rgbValue & 0xFF))/255.0
alpha:1.0];
}
// As an Objective-C function:
- (UIColor *)UIColorFromRGB:(NSInteger)rgbValue {
return [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
green:((float)((rgbValue & 0xFF00) >> 8))/255.0
blue:((float)(rgbValue & 0xFF))/255.0
alpha:1.0];
}
If you decide to stick with the macro, though, you should put parentheses around rgbValue wherever it appears. If I decide to call your macro with:
UIColorFromRGB(0xFF0000 + 0x00CC00 + 0x000099);
you may run into trouble.
The last bit of code is certainly the most readable, but probably the least portable - you can't call it simply from anywhere in your program.
All in all, I'd suggest refactoring your macro into a function and leaving it at that.
I typically recommend functions rather than complex #defines. If inlining has a real benefit, the compiler will generally do it for you. #defines make debugging difficult, particularly when they're complex (and this one is).
But there's nothing wrong with using a function here. The only nitpick I'd say is that you should be using CGFloat rather than float, but there's nothing wrong with the hex notation if it's more comfortable for you. If you have a lot of these, I can see where using Web color notation may be convenient. But avoid macros.
I.m.h.o the UIcolor method is more readable. I think macro's are great if they solve a problem; i.e. provide more performance and/or readable code.
It is not clear to me what the advantage of using a macro is in this case, so I'd prefer the second option.
Keep in mind that 33 != 0x33. The first is decimal notation and the second is hexadecimal. They're both valid, but they are different. Your second option should read
cell.textColor = [UIColor colorWithRed:0x66/255.0
green:0x33/255.0
blue:0x33/255.0
alpha:1.0];
or
cell.textColor = [UIColor colorWithRed:102/255.0
green:51/255.0
blue:51/255.0
alpha:1.0];
Nice Marius, but to compile I had to get rid of the parenthesis, as follows (otherwise, Objective C takes it literally and you get a syntax compilation error:
#define RGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
...
NSArray *palette;
...
palette = [NSArray arrayWithObjects:
RGB(0,0,0),
RGB(255,0,0), // red
...