1

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

http://www.colorpicker.com/

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"

4

1 回答 1

1

那么,这实际上涉及三种不同的情况。首先,05 只是 5,无论是十六进制还是十进制。在这种情况下,问题不在于您的输入,而可能在于输出。但是,请记住这是一个十进制文字,因此如果您想#101010 表示每种颜色的 255 种中的 16 种,请使用十六进制常量,例如0x10.

第二:关于输出。您使用的格式字符串不会在每种颜色中强制使用 2 位数字,这就是为什么您的05成为 just 5. %02x您可以通过使用而不是强制将整数(十六进制形式)输出为用零填充的两位数字%x

第三,其实是第一。看起来您的方法有效,但输出错误。修复格式字符串应该可以解决这个问题。无论如何,这是使用sscanf.

- (void)interpretHexString(NSString *str)
{
    int r,g,b;
    const char *s = [str cStringUsingEncoding:NSUTF8StringEncoding];

    if (sscanf(s, "#%02x%02x%02x", &r, &g, &b) != 3) {
        /* problem with input */
    } else {
        /* set ivars */
        [self setRed:r];
        [self setGreen:g];
        [self setBlue:b];
    }
}
于 2012-11-12T13:47:14.843 回答