Question

I'm trying to create a UIColor object according to the value of the current temperature. Here's a PNG image scale whose color from left to right I would like to associate to increasing temperature.

enter image description here

I found this PHP function which does exactly this, returning a color value in RGB, where the file "colors.png" is the one I posted above.

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);

}

My question is, how can I implement something similar to this function in Objective-C returning a UIColor object?

Thank you so much for your help!

Was it helpful?

Solution

Create UIImage Category

#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];
}

Then simply use the following

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top