If I take a photo of a white block that reads RGB of (255,255,255) and an HSV of (0,0,100).
Now I take a second photo of a purple block inside of different lighting conditions the RGB turns out to be (79, 40, 145) and an HSV of (262,72,56). Now since the fact that their values are different (different lighting conditions) in their HSV it is apparent that their RGB values cannot be accurately compared to test.
So therefore I need to use the HSV. So in order to scale "up" my HSV I need to multiply the purple value by 100/56 to bring it up to 100. Therefore the resulting color is HSV (262, 72, 100). Now. This is great and dandy - but is there a way in Objective-C to read the HSV back into RGB or convert the HSV into RGB? I cannot seem to find any good resources on the matter.
I am not sure what to read my color with but here is example code of what I am trying to accomplish:
CIColor *myColor = [CIColor colorWithRed:rn green:gn blue:bn alpha:1];
where rn
,gn
, and bn
are the RGB of my purple.
Now what I want to do is get the HSV from that CIColor object.
hn = myColor.hue;
sn = myColor.saturation;
vn = myColor.value;
Next I want to take that HSV and scale it up to the white like such
CIColor *myNewColor = [CIColor colorWithHue:hn, saturation:sn, value:vn*(vw/vn)];
and change it back to RGB.
Sorry if this is confusing - perhaps there is a better way to go about this.
EDIT 2:
rw = [self.whiteRGBValues.red floatValue];
gw = [self.whiteRGBValues.green floatValue];
bw = [self.whiteRGBValues.blue floatValue];
hw = [self.whiteRGBValues.hue floatValue];
sw = [self.whiteRGBValues.sat floatValue];
vw = [self.whiteRGBValues.val floatValue];
NSLog([NSString stringWithFormat:@"%f,%f,%f,%f,%f,%f",rw,gw,bw,rn,gn,bn]);
float scalar = vw/vn;
UIColor *myColor = [UIColor colorWithRed:rn green:gn blue:bn alpha:1];
CGFloat myHue, mySat, myBright, myAlpha;
[myColor getHue:&myHue saturation:&mySat brightness:&myBright alpha:&myAlpha];
UIColor *scaledColor = [UIColor colorWithHue:myHue
saturation:mySat
brightness:myBright*scalar
alpha:myAlpha];
CGFloat myScaledR, myScaledG, myScaledB, myScaledAlpha;
CGFloat myScaledHue, myScaledSat, myScaledBright;
[scaledColor getRed:&myScaledR green:&myScaledG blue:&myScaledB alpha:&myScaledAlpha];
self.rgbScaledValues.red = [[NSNumber numberWithFloat:myScaledR] stringValue];
self.rgbScaledValues.green = [[NSNumber numberWithFloat:myScaledG] stringValue];
self.rgbScaledValues.blue = [[NSNumber numberWithFloat:myScaledB] stringValue];
[scaledColor getHue:&myScaledHue saturation:&myScaledSat brightness:&myScaledBright alpha:&myScaledAlpha];
self.rgbScaledValues.hue = [[NSNumber numberWithFloat:myScaledHue] stringValue];
self.rgbScaledValues.sat = [[NSNumber numberWithFloat:myScaledSat] stringValue];
self.rgbScaledValues.val = [[NSNumber numberWithFloat:myScaledBright] stringValue];
I get an error thrown with breakpoint on the line with
`UIColor *myColor = [UIColor colorWithRed:rn green:gn blue:bn alpha:1];`
You can get the HSB and RGB components by using the built-in methods. First, convert your CIColor
to a UIColor
.
UIColor *color = [UIColor colorWithCIColor: myColor];
CGFloat hue;
CGFloat saturation;
CGFloat brightness;
CGFloat alpha;
[color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
// those 4 floats now hold the HSB values of this color
CGFloat red;
CGFloat green;
CGFloat blue;
CGFloat alpha2;
[color getRed:&red green:&green blue:&blue alpha:&alpha2];
// those 4 floats now hold the RGB values of this color
From there, you can now do whatever you want with these values including manipulating them to create new colors.