This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRSColorFunctions.m
executable file
·47 lines (41 loc) · 1.64 KB
/
RSColorFunctions.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// RSColorFunctions.m
// RSColorPicker
//
// Created by Ryan Sullivan on 3/12/13.
// Copyright (c) 2013 Freelance Web Developer. All rights reserved.
//
#import "RSColorFunctions.h"
BMPixel RSPixelFromHSV(CGFloat H, CGFloat S, CGFloat V)
{
UIColor *color = [UIColor colorWithHue:H saturation:S brightness:V alpha:1];
CGFloat r, g, b;
[color getRed:&r green:&g blue:&b alpha:NULL];
return BMPixelMake(r, g, b, 1.0);
}
void RSHSVFromPixel(BMPixel pixel, CGFloat *h, CGFloat *s, CGFloat *v)
{
UIColor *color = [UIColor colorWithRed:pixel.red green:pixel.green blue:pixel.blue alpha:1];
[color getHue:h saturation:s brightness:v alpha:NULL];
}
void RSGetComponentsForColor(float components[4], UIColor *color) {
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char resultingPixel[4];
CGContextRef context = CGBitmapContextCreate(&resultingPixel, 1, 1, 8, 4, rgbColorSpace, kCGImageAlphaPremultipliedLast);
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, CGRectMake(0, 0, 1, 1));
CGContextRelease(context);
CGColorSpaceRelease(rgbColorSpace);
for (int component = 0; component < 4; component++) {
components[component] = resultingPixel[component] / 255.0f;
}
}
CGSize RSCGSizeWithScale(CGSize size, CGFloat scale) {
return CGSizeMake(size.width * scale, size.height * scale);
}
CGPoint RSCGPointWithScale(CGPoint point, CGFloat scale) {
return CGPointMake(point.x * scale, point.y * scale);
}
UIImage* RSUIImageWithScale(UIImage *img, CGFloat scale) {
return [UIImage imageWithCGImage:img.CGImage scale:scale orientation:UIImageOrientationUp];
}