UIColor来自温度值

时间:2014-05-15 08:24:01

标签: php ios objective-c xcode uicolor

我试图根据当前温度的值创建一个UIColor对象。这是一个PNG图像比例,其颜色从左到右我想与升高的温度联系起来。

enter image description here

我发现这个PHP函数就是这样做的,返回RGB中的颜色值,其中文件" colors.png"是我上面发布的那个。

function getWeatherColor($temperature) {

    $today = ($temperature/100);

    $percent = ($today*100);

    //1500 is the width in px of the base image that we use
    $color = $today*1500-1;

    $im = imagecreatefrompng("colors.png");
    $rgb = imagecolorat($im, $color, 0);
    $r = ($rgb >> 16) & 0xFF;
    $g = ($rgb >> 8) & 0xFF;
    $b = $rgb & 0xFF;

    //Return the rgb value
    return rgb($r,$g,$b);

}

我的问题是,如何在Objective-C中实现与此函数类似的返回UIColor对象的内容?

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

创建UIImage类别

#import <CoreGraphics/CoreGraphics.h>

#import "UIImage+ColorAtPixel.h"

@implementation UIImage (ColorAtPixel)

- (UIColor *)colorForTemperature:(CGFloat)temperature{

   CGFloat xCoord = (temperature/100)*(self.size.width-1);
   CGFloat yCoord = self.size.height/2;

   //coordinates for 1 pixel on the image computed from the temperature value
   CGPoint point = CGPointMake(xCoord,yCoord);



    // Create a 1x1 pixel byte array and bitmap context to draw the pixel into.
    // Reference: http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage
    NSInteger pointX = trunc(point.x);
    NSInteger pointY = trunc(point.y);
    CGImageRef cgImage = self.CGImage;
    NSUInteger width = CGImageGetWidth(cgImage);
    NSUInteger height = CGImageGetHeight(cgImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * 1;
    NSUInteger bitsPerComponent = 8;
    unsigned char pixelData[4] = { 0, 0, 0, 0 };
    CGContextRef context = CGBitmapContextCreate(pixelData, 
                                                 1,
                                                 1,
                                                 bitsPerComponent, 
                                                 bytesPerRow, 
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);

    // Draw the pixel we are interested in onto the bitmap context
    CGContextTranslateCTM(context, -pointX, -pointY);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
    CGContextRelease(context);

    // Convert color values [0..255] to floats [0.0..1.0]
    CGFloat red   = (CGFloat)pixelData[0] / 255.0f;
    CGFloat green = (CGFloat)pixelData[1] / 255.0f;
    CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;
    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

然后只需使用以下

CGFloat *temperature = 35.8; // Celsius it will work from 0 to 100 degrees with this calculation. Feel free to adjust it to your needs
UIImage *colors = [UIImage imageNamed:@"colors.png"];
UIColor *tempColor = [colors colorForTemperature:temperature];