【问题标题】:Setting Device Independent RGB on iOS在 iOS 上设置与设备无关的 RGB
【发布时间】:2015-05-08 14:32:31
【问题描述】:

我正在检测点击像素的 rgb。不同的 iPad 返回的 RGB 值略有不同。我维护每个设备返回的不同值的 plist,当应用程序打开时,它确定我使用的设备并使用适当的值。这是一个糟糕的解决方案 - 但它确实有效。

我现在想正确解决这个问题,所以我深入研究了 iOS 上的色彩空间。看来我可以使用 CGColorSpaceCreateCalibratedRGB 来设置标准 RGB 而不管设备如何,所以返回的值是相同的?还是我需要转换

但是,我不知道跨设备创建标准颜色空间所需的任何值,因此我的像素颜色返回值始终相同 - 或者如果这是可能的。

一些当前示例返回值:
iPad 2 r31 g0 b133 a1
iPad Air r30 g0 b132 a1

谁能帮助以独立于设备的方式“标准化”像素返回值?

- (UIColor*) getPixelColorAtLocation:(CGPoint)point {
    UIColor* color = nil;
    CGImageRef inImage = self.image.CGImage;
    // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpha, Red, Green, Blue
    CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
    if (cgctx == NULL) { return nil; /* error */ }

    size_t w = CGImageGetWidth(inImage);
    size_t h = CGImageGetHeight(inImage);
    CGRect rect = {{0,0},{w,h}}; 

    // Draw the image to the bitmap context. Once we draw, the memory 
    // allocated for the context for rendering will then contain the 
    // raw image data in the specified color space.
    CGContextDrawImage(cgctx, rect, inImage); 

    // Now we can get a pointer to the image data associated with the bitmap
    // context.
    unsigned char* data = CGBitmapContextGetData (cgctx);
    if (data != NULL) {
        //offset locates the pixel in the data from x,y. 
        //4 for 4 bytes of data per pixel, w is width of one row of data.
        int offset = 4*((w*round(point.y))+round(point.x));
        int alpha =  data[offset]; 
        int red = data[offset+1]; 
        int green = data[offset+2]; 
        int blue = data[offset+3]; 
        ////NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);
        //NSLog(@"colors: RGB A %i %i %i  %i",red,green,blue,alpha);
        color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
    }


    // When finished, release the context
    CGContextRelease(cgctx); 
    // Free image data memory for the context
    if (data) { free(data); }

    return color;
}


- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {

    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    // Get image width, height. We'll use the entire image.
    size_t pixelsWide = CGImageGetWidth(inImage);
    size_t pixelsHigh = CGImageGetHeight(inImage);

    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.
    bitmapBytesPerRow   = (pixelsWide * 4);
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

    // Use the generic RGB color space.
    colorSpace = CGColorSpaceCreateDeviceRGB();

    if (colorSpace == NULL)
    {
        fprintf(stderr, "Error allocating color space\n");
        return NULL;
    }

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL) 
    {
        fprintf (stderr, "Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }

    // Create the bitmap context. We want pre-multiplied ARGB, 8-bits 
    // per component. Regardless of what the source image format is 
    // (CMYK, Grayscale, and so on) it will be converted over to the format
    // specified here by CGBitmapContextCreate.
    context = CGBitmapContextCreate (bitmapData,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedFirst);
    if (context == NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
    }

    // Make sure and release colorspace before returning
    CGColorSpaceRelease( colorSpace );

    return context;
}

【问题讨论】:

    标签: ios objective-c ipad pixel color-space


    【解决方案1】:

    如前所述,它在 iOS 上不受支持。我也真的会用!

    但是,您可以尝试不同的方法。我要做的是将颜色转换为 HSV 空间(或其他类似的感知空间),并在您的检查中设置一个范围。检查精确的颜色与检查浮点数的精确相等性相同。这不是一个好主意。

    您可以使用[-UIColor getHue:saturation:brightness:alpha:] (docs here) 获取UIColor 的HSV 组件。 Hue 在 0-1 范围内,但它代表一个角度,因此找到“接近度”需要一点数学。但是saturation 和value 工作正常,因此绝对差异应该足以确定两种颜色彼此之间的接近程度。

    【讨论】:

      【解决方案2】:

      不幸的是,iOS 没有颜色管理,因此设备独立颜色空间在 iOS 上将无法正常工作。我也一直在尝试这样做,iOS 确实不支持它。在 OS X 上,这是受支持的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-13
        • 1970-01-01
        • 1970-01-01
        • 2018-01-31
        • 1970-01-01
        • 2013-08-30
        相关资源
        最近更新 更多