I have a colour class set up where it takes red/blue/green values and uses them to create a hex string which is then used to return a colour. It can be initialized by providing a red, blue and green colour like this
Colour *col = [[Colour alloc] initWithRed:200 Green:100 Blue:05];
The problem with this method is, if I pass in a value like 05, the 0 is stripped off, and just a 5 is passed in, so the blue will set to just be 5 rather than 05. My method to return a valid hex string is this
-(NSString *)getHexString
{
NSString *hexString = [NSString stringWithFormat:@"#%x%x%x", iRed_i,iGreen_i,iBlue_i];
hexString = [hexString uppercaseString];
return hexString;
}
It is expecting 2 values for each number to return the correct colour code, but the 0 is being stripped off meaning it returns an incorrect colour.
The other method I use to create a colour is to initialise the colour object with a hex string like this:
Colour *colour = [[Colour alloc] initWithHex:@"#782402"];
I then use scanner to separate the 3 values like so
if ([sHex_p hasPrefix:@"#"]) sHex_p = [sHex_p substringFromIndex:1];
unsigned int outVal;
NSString *sRed = [sHex_p substringToIndex:2];
NSScanner* scanner = [NSScanner scannerWithString:sRed];
[scanner scanHexInt:&outVal];
[self setRed:outVal];
NSString *sGreen = [[sHex_p substringFromIndex:2] substringToIndex:2];
scanner = [NSScanner scannerWithString:sGreen];
[scanner scanHexInt:&outVal];
[self setGreen:outVal];
NSString *sBlue = [sHex_p substringFromIndex:4];
scanner = [NSScanner scannerWithString:sBlue];
[scanner scanHexInt:&outVal];
[self setBlue:outVal];
}
But again same problem, in the hex string I provided the last 2 values are 02, but when converting from int to string, that 0 will be stripped out, so just a 2 will be passed, again ending up with an incorrect colour.
I really am unsure as what the best way to solve this is. Would be really grateful if someone could be point in the right direction.
I have been testing it using this site here
Quick example I am trying to create this hex string “#DB4200”. It requires red = 219 green = 66, blue = 00
But as the blue is just set to 0 when converts from string to int, ends up returning an incorrect colour, hex string ends up as “#DB420”
Well, this actually involves three different situations. First, 05 is just 5,
in both hexadecimal and decimal bases. The problem is not with your input in
this case, but probably with the output. However, remember this is a decimal
literal, so if you want
#101010to mean 16 out of 255 of each color, usehexadecimal constants like
0x10.Second: Regarding output. The format string you’re using does not enforce 2
digits in each color, and that’s why your
05becomes just5. You can forcethe output of an integer (in hexadecimal form) to two digits padded with zeros
by using
%02xinstead of%x.Third, is actually first. Looks like your method is working, but the output is
wrong. Fixing the format string should solve this. Anyway, here’s an
alternative version using
sscanf.