-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUIColor+YUVSpace.m
executable file
·130 lines (99 loc) · 3.44 KB
/
UIColor+YUVSpace.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// UIColor+LEColorPicker.m
// LEColorPicker
//
// Created by Luis Enrique Espinoza Severino on 03-12-12.
// Copyright (c) 2012 Luis Espinoza. All rights reserved.
//
#import "UIColor+YUVSpace.h"
#import <float.h>
@implementation UIColor (YUVSpace)
+ (float)yComponentFromColor:(UIColor *)color
{
float red = 0.0;
float green = 0.0;
float blue = 0.0;
float alpha = 0.0;
float y = 0.0;
if (color) {
size_t numComponents = CGColorGetNumberOfComponents(color.CGColor);
if (numComponents == 4) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
red = components[0];
green = components[1];
blue = components[2];
alpha = components[3];
}
y = 0.299*red + 0.587*green+ 0.114*blue;
}
return y;
}
+ (float)uComponentFromColor:(UIColor *)color
{
float red = 0.0;
float green = 0.0;
float blue = 0.0;
float alpha = 0.0;
float u = 0.0;
if (color) {
size_t numComponents = CGColorGetNumberOfComponents(color.CGColor);
if (numComponents == 4) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
red = components[0];
green = components[1];
blue = components[2];
alpha = components[3];
}
u = (-0.14713)*red + (-0.28886)*green + (0.436)*blue;
}
return u;
}
+ (float)vComponentFromColor:(UIColor *)color
{
float red = 0.0;
float green = 0.0;
float blue = 0.0;
float alpha = 0.0;
float v = 0.0;
if (color) {
size_t numComponents = CGColorGetNumberOfComponents(color.CGColor);
if (numComponents == 4) {
const CGFloat *components = CGColorGetComponents(color.CGColor);
red = components[0];
green = components[1];
blue = components[2];
alpha = components[3];
}
v = 0.615*red + (-0.51499)*green + (-0.10001)*blue;
}
return v;
}
+ (float)YUVSpaceDistanceToColor:(UIColor *)toColor fromColor:(UIColor *)fromColor
{
float YToColor = [UIColor yComponentFromColor:toColor];
float UToColor = [UIColor uComponentFromColor:toColor];
float VToColor = [UIColor vComponentFromColor:toColor];
float YFromColor = [UIColor yComponentFromColor:fromColor];
float UFromColor = [UIColor uComponentFromColor:fromColor];
float VFromColor = [UIColor vComponentFromColor:fromColor];
float deltaY = YToColor - YFromColor;
float deltaU = UToColor - UFromColor;
float deltaV = VToColor - VFromColor;
float distance = sqrtf(deltaY*deltaY + deltaU*deltaU + deltaV*deltaV);
return distance;
}
+ (float)YUVSpaceSquareDistanceToColor:(UIColor *)toColor fromColor:(UIColor *)fromColor
{
float YToColor = [UIColor yComponentFromColor:toColor];
float UToColor = [UIColor uComponentFromColor:toColor];
float VToColor = [UIColor vComponentFromColor:toColor];
float YFromColor = [UIColor yComponentFromColor:fromColor];
float UFromColor = [UIColor uComponentFromColor:fromColor];
float VFromColor = [UIColor vComponentFromColor:fromColor];
float deltaY = YToColor - YFromColor;
float deltaU = UToColor - UFromColor;
float deltaV = VToColor - VFromColor;
float distance = deltaY*deltaY + deltaU*deltaU + deltaV*deltaV;
return distance;
}
@end