Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 2个值应相同,但不是';T_Iphone_Objective C_Hex_Uicolor - Fatal编程技术网

Iphone 2个值应相同,但不是';T

Iphone 2个值应相同,但不是';T,iphone,objective-c,hex,uicolor,Iphone,Objective C,Hex,Uicolor,我有以下代码,它接受某人输入的十六进制代码,并将其转换为HSB: NSString *cString = [[hexText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString]; // String should be 6 or 8 characters if ([cString length] < 6) NSLog(

我有以下代码,它接受某人输入的十六进制代码,并将其转换为HSB:

NSString *cString = [[hexText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    // String should be 6 or 8 characters
    if ([cString length] < 6) NSLog(@"UH OH");
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
    if ([cString length] != 6) NSLog(@"UH OH");
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [cString substringWithRange:range];
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    float red = r / 255.0f;
    float green = g / 255.0f;
    float blue = b / 255.0f;

    float colorArray[3];
    colorArray[0] = red; 
    colorArray[1] = green; 
    colorArray[2] = blue;
    int max;
    int min;

    min=0;
    max=0;
    for(int i=1; i<3; i++)
    {
        if(colorArray[i] > colorArray[max])
            max=i;
        if(colorArray[i] < colorArray[min])
            min=i;
    }

    if(max==min)
    {
        h3=0;
        s3=0;
        b3=colorArray[0];
    }
    else
    {
        b3=colorArray[max];

        s3=(colorArray[max]-colorArray[min])/(colorArray[max]);

        if(max==0) // Red
            h3 = (colorArray[1]-colorArray[2])/(colorArray[max]-colorArray[min])*60/360;
        else if(max==1) // Green
            h3 = (2.0 + (colorArray[2]-colorArray[0])/(colorArray[max]-colorArray[min]))*60/360;
        else // Blue
            h3 = (4.0 + (colorArray[0]-colorArray[1])/(colorArray[max]-colorArray[min]))*60/360;
    }
NSString*cString=[[hexText.text-stringByTrimmingCharactersInSet:[NSCharacterSet-whitespaceAndNewlineCharacterSet]]大写字符串];
//字符串应为6或8个字符
如果([cString长度]<6)NSLog(@“UH-OH”);
//如果出现,请删除0X
如果([cString hasPrefix:@“0X]”)cString=[cString substringFromIndex:2];
如果([cString length]!=6)NSLog(@“UH-OH”);
//分为r、g、b子串
NSRange范围;
range.location=0;
range.length=2;
NSString*rString=[cString substringWithRange:range];
range.location=2;
NSString*gString=[cString substringWithRange:range];
range.location=4;
NSString*bString=[cString substringWithRange:range];
//扫描值
无符号整数r,g,b;
[[NSScanner scannerWithString:rsString]scanHexInt:&r];
[[NSScanner scannerWithString:gString]scanHexInt:&g];
[[NSScanner SCANNER WITHSTRING:bString]scanHexInt:&b];
浮动红=r/255.0f;
绿色浮动=g/255.0f;
蓝色浮动=b/255.0f;
浮点彩色数组[3];
colorArray[0]=红色;
colorArray[1]=绿色;
colorArray[2]=蓝色;
int max;
int-min;
最小值=0;
max=0;
对于(int i=1;i colorArray[max])
max=i;
if(colorArray[i]
然后,我有一个代码,它的作用正好相反——将HSB转换为十六进制代码:

    UIColor *forC = colourPreview.backgroundColor;

const CGFloat *c = CGColorGetComponents([forC CGColor]);

    CGFloat r, g, b;  
    r = c[0];  
    g = c[1];  
    b = c[2];  

    if (r < 0.0f) r = 0.0f;  
    if (g < 0.0f) g = 0.0f;  
    if (b < 0.0f) b = 0.0f;  

    if (r > 1.0f) r = 1.0f;  
    if (g > 1.0f) g = 1.0f;  
    if (b > 1.0f) b = 1.0f;  

    hexWithoutHash = [NSString stringWithFormat:@"%02X%02X%02X",  
                        (int)(r * 255), (int)(g * 255), (int)(b * 255)];  
UIColor*forC=colorPreview.backgroundColor;
const CGFloat*c=CGColorGetComponents([forC CGColor]);
cgr,g,b;
r=c[0];
g=c[1];
b=c[2];
如果(r<0.0f)r=0.0f;
如果(g<0.0f)g=0.0f;
如果(b<0.0f)b=0.0f;
如果(r>1.0f)r=1.0f;
如果(g>1.0f)g=1.0f;
如果(b>1.0f)b=1.0f;
hexWithoutHash=[NSString stringWithFormat:@“%02X%02X%02X”,
(int)(r*255),(int)(g*255),(int)(b*255)];

这两者都应该给出相同的值,而且大多数情况下都是这样。但有时我会输入一个十六进制代码,比如208DBC,它会返回1F8CBC。有什么想法吗?我认为这与第二位代码返回不准确的十六进制代码有关,但不确定在这种情况下如何使其更准确?

可能是浮点精度问题。使用
float
double
不会像使用
int
long
那样存储精确的值。它存储的精确值与所允许的最接近。存储值和精确值之间的差异通常非常小,但可能大到当您转换回整数时,您的值会被截断为下一个较小的整数。这就是您的输出中发生的情况(即
0x1F=0x20-1
0x8C=0x8D-1

以下代码可能说明了该问题:

for (int redColor = 0; redColor < 256; redColor++) {
    int originalRed = redColor;
    float red = originalRed / 255.0f;
    float redMultiplied = red * 255;
    int newRed = (int)redMultiplied;

    if (newRed != originalRed) {
        NSLog(@"Value mismatch detected:  origianlRed=%d, red=%f, redMultiplied=%f, newRed=%d", 
              originalRed, red, redMultiplied, newRed);
    }
}
for(int-redColor=0;redColor<256;redColor++){
int originalRed=红色;
浮动红=原始红/255.0f;
浮点红乘=红色*255;
int newRed=(int)red乘;
如果(新红色!=原始红色){
NSLog(@“检测到值不匹配:原始红色=%d,红色=%f,红色相乘=%f,新红色=%d”,
原始红色、红色、红色、新红色);
}
}

啊,你打败了我。但是你的例子不够复杂,不足以造成精度损失,每次都是正确的。@ughoavgfhw-是的,当我实际运行它时,我很惊讶,没有得到任何错误。关于如何使其可靠地演示问题,有什么建议吗?那么我如何在代码中修复问题呢?使用long代替float并不能奏效。@aroth,这与本例并不相关,但是
sqrt(2)*sqrt(2)
是一个非常常见的例子。对于更相关的内容,您可以使用他的示例:当您使用这些数字计算h3并四舍五入为整数时,您得到140,这是0x8C感谢所有帮助人员。:-)