I think you have two problems. One is easy - how to create a colour and set it as your background, but this depends on solving the first, trickier, problem, how to map your analog value to a colour.
This code -
else if (data[i] == 0x0B) {
UInt16 Value;
Value = data[i+2] | data[i+1] << 8;
}
gets you the analog value as a 16 bit unsigned integer in Value
. Colors are typically created using a 8 bit triple (either RGB or HSB) plus a alpha or transparency value (but lets ignore that) - so you have to decide how to map a 16 bit value onto a 24 bit value. The best way to do this depends on exactly what you are trying to achieve and what the range of your input is (i.e. depending on your analog input source you may not get the full range from 0-65535).
One simple method is to map the high 8 bits to Hue (colour) and the low 8 bits to saturation (intensity) with a fixed brightness. To add a bit of complexity the UIColor
initialisation methods take floats in the range 0-1, so we need to divide 255 to scale the result
CGFloat hue=(Value >>8)/255.0;
CGFloat sat=(Value & 0xff)/255.0;
CGFloat bright=0.5;
UIColor *backGroundColor=[UIColor colorWithHue:hue saturation:sat brightness:bright alpha:1.0];
// Finally - set the background colour
self.view.backgroundColor=backgroundColor;
Note, that you already have the two bytes separately in data[i+1]
and data[i+2]
so you can save the bit masking & shifting and just use -
else if (data[i] == 0x0B)
{
CGFloat hue=data[i+1]/255.0;
CGFloat saturation=data[i+2]/255.0;
self.view.backgroundColor=[UIColor colorWithHue:hue saturation:saturation brightness:0.5 alpha:1.0];
}