Skip to content

Instantly share code, notes, and snippets.

@hmiorroi
Created June 13, 2017 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmiorroi/7ca8273e8a7b8ce6a4b26782f69c3402 to your computer and use it in GitHub Desktop.
Save hmiorroi/7ca8273e8a7b8ce6a4b26782f69c3402 to your computer and use it in GitHub Desktop.
color picker
//
// UIImage+Picker.h
//
// Created by hiro on 02/18/2013.
// Copyright (c) 2013 sign g.k. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (Picker)
-(NSString *)colorAtPosition:(CGPoint)position;
@end
//
// UIImage+Picker.m
//
// Created by hiro on 02/18/2013.
// Copyright (c) 2013 sign g.k. All rights reserved.
//
#import "UIImage+Picker.h"
@implementation UIImage (Picker)
-(NSString *)colorAtPosition:(CGPoint)position {
CGRect sourceRect = CGRectMake(position.x, position.y, 1.f, 1.f);
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, sourceRect);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *buffer = malloc(4);
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big;
CGContextRef context = CGBitmapContextCreate(buffer, 1, 1, 8, 4, colorSpace, bitmapInfo);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0.f, 0.f, 1.f, 1.f), imageRef);
CGImageRelease(imageRef);
CGContextRelease(context);
CGFloat r = buffer[0] / 255.f;
CGFloat g = buffer[1] / 255.f;
CGFloat b = buffer[2] / 255.f;
CGFloat a = buffer[3] / 255.f;
free(buffer);
return [NSString stringWithFormat:@"%f,%f,%f,%f", r, g, b, a];
// return [UIColor colorWithRed:r green:g blue:b alpha:a];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment