-1

How can you convert a RGB color to an 8 bit (0-255) color (for a Limitless LED light bulb) in objective c? so output is in the format (0-255), convert from 16777216 to 256 in hex, instead of range 000000-FFFFFF, to range 00-FF

NSString *output = nil;
NSColor  *color  = [[self.colorWell color] colorUsingColorSpaceName:NSCalibratedRGBColorSpace];

double redFloatValue, greenFloatValue, blueFloatValue;

[color getRed:&redFloatValue green:&greenFloatValue blue:&blueFloatValue alpha:NULL];

int redIntValue, greenIntValue, blueIntValue;

redIntValue   = redFloatValue * 255;
greenIntValue = greenFloatValue * 255;
blueIntValue  = blueFloatValue * 255;

output = [NSString stringWithFormat:@"#%02x%02x%02x", redIntValue, greenIntValue, blueIntValue];

NSLog(@"%@", output);

With a color well when you open a color picker, you can pick 16,8 million colors which I need to translate to 256 colors.

This is a discussion of the colors from http://limitlessled.com/dev

Byte2: Color Matrix Chart: (thanks Stephan Schaade, http://knx-user-forum.de http://mknx.github.io/smarthome/)

note there are more colours (0-255) in between, this color chart is just steps of 16.
    0x00 Violet
    0x10 Royal_Blue
    0x20 Baby_Blue
    0x30 Aqua
    0x40 Mint
    0x50 Seafoam_Green
    0x60 Green
    0x70 Lime_Green
    0x80 Yellow
    0x90 Yellow_Orange
    0xA0 Orange
    0xB0 Red
    0xC0 Pink
    0xD0 Fusia
    0xE0 Lilac
    0xF0 Lavendar
6
  • 3
    Not sure what you're asking, because the source code you posted looks like it does exactly what you want
    – chedabob
    May 17, 2015 at 15:39
  • Please clarify what issue you are having with the posted code.
    – rmaddy
    May 17, 2015 at 15:41
  • BTW - be sure to check the return value of the getRed:green:blue:alpha: method. It can fail and you need to handle that case.
    – rmaddy
    May 17, 2015 at 15:42
  • See stackoverflow.com/questions/12338015/…
    – rmaddy
    May 17, 2015 at 15:59
  • 2
    No, it's not. There is not a single, well-defined coding scheme for representing colors as a 8-bit value. There are many. You have to go to the vendor of the light to get information about their encoding scheme. Worse, even within a particular brand, there might be different color representations depending upon the light. For example, with Philips Hue, there are three different gamuts for their product line, each which translates to a different representation of the color. BTW, have you checked the light's vendor to see if they have API that converts for you? Philips does.
    – Rob
    May 17, 2015 at 16:17

2 Answers 2

1

The Limitless LED development page you reference has a table, mapping these values to web color strings (which are hex representations of red, green, and blue):

"Violet",       0x00, "#EE82EE"
"RoyalBlue",    0x10, "#4169E1"
"LightSkyBlue", 0x20, "#87CEFA"
"Aqua",         0x30, "#00FFFF"
"AquaMarine",   0x40, "#7FFFD4"
"SeaGreen",     0x50, "#2E8B57"
"Green",        0x60, "#008000"
"LimeGreen",    0x70, "#32CD32"
"Yellow",       0x80, "#FFFF00"
"Goldenrod",    0x90, "#DAA520"
"Orange",       0xA0, "#FFA500"
"Red",          0xB0, "#FF0000"
"Pink",         0xC0, "#FFC0CB"
"Fuchsia",      0xD0, "#FF00FF"
"Orchid",       0xE0, "#DA70D6"
"Lavender",     0xF0, "#E6E6FA"

You theoretically could write a routine that compared the red/green/blue values that you obtained from the code in your question and see to which of these entries it was closest. Also, there's an argument for doing this analysis using HSB rather than RGB (since brightness is another control you have, so you might want to pull that dimension out of the "which color is closest" algorithm).

But this all seems like a poor solution. It's not entirely clear as to whether this table is correct (i.e. is this an official mapping of colors, or is this just the product of some random developer who tried to map between the names of the colors and standard web representations). As you point out, if nothing else, the table certainly isn't complete (if you're stuck with this, you might want to use linear interpolation to fill in all of the missing values).

Regardless, before going further, I'd suggest contacting the LED provider directly and see if they can provide a more definitive algorithm for converting from the RGB values to their one-byte code. It would be a shame to cobble something together from such imprecise information, especially since this is a problem that they or one of their developers may have already tackled.

0

Just in case someone else comes looking for a Color Matrix as I did... I made up a list of the 255 LimitlessLED colors: Milight_AR_Bulb

else if (colorIndicator < 99999) sendCustomCommand("C200") //WHITE
else if (colorIndicator < 101100) sendCustomCommand("40AF")
else if (colorIndicator < 102050) sendCustomCommand("40AE")
else if (colorIndicator < 102100) sendCustomCommand("40AD")
else if (colorIndicator < 103100) sendCustomCommand("40AC")
else if (colorIndicator < 104050) sendCustomCommand("40AB")
else if (colorIndicator < 104100) sendCustomCommand("40AA")
else if (colorIndicator < 105100) sendCustomCommand("40A9")
else if (colorIndicator < 106050) sendCustomCommand("40A8")
else if (colorIndicator < 106100) sendCustomCommand("40A7")
else if (colorIndicator < 107100) sendCustomCommand("40A6")
else if (colorIndicator < 108050) sendCustomCommand("40A5")
else if (colorIndicator < 108100) sendCustomCommand("40A4")
else if (colorIndicator < 109100) sendCustomCommand("40A3")
else if (colorIndicator < 110050) sendCustomCommand("40A2")
else if (colorIndicator < 110100) sendCustomCommand("40A1")
else if (colorIndicator < 111050) sendCustomCommand("40A0")
else if (colorIndicator < 111100) sendCustomCommand("409F")
else if (colorIndicator < 112020) sendCustomCommand("409E")
else if (colorIndicator < 112040) sendCustomCommand("409D")
else if (colorIndicator < 112060) sendCustomCommand("409C")
else if (colorIndicator < 112080) sendCustomCommand("409B")
else if (colorIndicator < 112100) sendCustomCommand("409A")
else if (colorIndicator < 113025) sendCustomCommand("4099")
else if (colorIndicator < 113050) sendCustomCommand("4098")
else if (colorIndicator < 113075) sendCustomCommand("4097")
else if (colorIndicator < 113100) sendCustomCommand("4096")
else if (colorIndicator < 114025) sendCustomCommand("4095")
else if (colorIndicator < 114050) sendCustomCommand("4094")
else if (colorIndicator < 114075) sendCustomCommand("4093")
else if (colorIndicator < 114100) sendCustomCommand("4092")
else if (colorIndicator < 115015) sendCustomCommand("4091")
else if (colorIndicator < 115030) sendCustomCommand("4090")
else if (colorIndicator < 115045) sendCustomCommand("408F")
else if (colorIndicator < 115060) sendCustomCommand("408E")
else if (colorIndicator < 115085) sendCustomCommand("408D")
else if (colorIndicator < 115100) sendCustomCommand("408C")
else if (colorIndicator < 116030) sendCustomCommand("408B")
else if (colorIndicator < 116040) sendCustomCommand("408A")
else if (colorIndicator < 116050) sendCustomCommand("4089")
else if (colorIndicator < 116060) sendCustomCommand("4088")
else if (colorIndicator < 116070) sendCustomCommand("4087")
else if (colorIndicator < 116080) sendCustomCommand("4086")
else if (colorIndicator < 116090) sendCustomCommand("4085")
else if (colorIndicator < 116100) sendCustomCommand("4084")
else if (colorIndicator < 117025) sendCustomCommand("4083")
else if (colorIndicator < 117050) sendCustomCommand("4082")
else if (colorIndicator < 117075) sendCustomCommand("4081")
else if (colorIndicator < 117100) sendCustomCommand("4080")
else if (colorIndicator < 119100) sendCustomCommand("407F")
else if (colorIndicator < 120100) sendCustomCommand("407E")
else if (colorIndicator < 121100) sendCustomCommand("407D")
else if (colorIndicator < 122100) sendCustomCommand("407C")
else if (colorIndicator < 123100) sendCustomCommand("407B")
else if (colorIndicator < 124100) sendCustomCommand("407A")
else if (colorIndicator < 125100) sendCustomCommand("4079")
else if (colorIndicator < 126100) sendCustomCommand("4078")
else if (colorIndicator < 127100) sendCustomCommand("4077")
else if (colorIndicator < 128100) sendCustomCommand("4076")
else if (colorIndicator < 129100) sendCustomCommand("4075")
else if (colorIndicator < 130100) sendCustomCommand("4074")
else if (colorIndicator < 131100) sendCustomCommand("4073")
else if (colorIndicator < 132100) sendCustomCommand("4072")
else if (colorIndicator < 133100) sendCustomCommand("4071")
else if (colorIndicator < 134100) sendCustomCommand("4070")
else if (colorIndicator < 135015) sendCustomCommand("406F")
else if (colorIndicator < 135030) sendCustomCommand("406E")
else if (colorIndicator < 135045) sendCustomCommand("406D")
else if (colorIndicator < 135060) sendCustomCommand("406C")
else if (colorIndicator < 135085) sendCustomCommand("406B")
else if (colorIndicator < 135100) sendCustomCommand("406A")
else if (colorIndicator < 136020) sendCustomCommand("4069")
else if (colorIndicator < 136040) sendCustomCommand("4068")
else if (colorIndicator < 136060) sendCustomCommand("4067")
else if (colorIndicator < 136080) sendCustomCommand("4066")
else if (colorIndicator < 136100) sendCustomCommand("4065")
else if (colorIndicator < 137020) sendCustomCommand("4064")
else if (colorIndicator < 137040) sendCustomCommand("4063")
else if (colorIndicator < 137060) sendCustomCommand("4062")
else if (colorIndicator < 137080) sendCustomCommand("4061")
else if (colorIndicator < 137100) sendCustomCommand("4060")
else if (colorIndicator < 138030) sendCustomCommand("405F")
else if (colorIndicator < 138060) sendCustomCommand("405E")
else if (colorIndicator < 138100) sendCustomCommand("405D")
else if (colorIndicator < 139030) sendCustomCommand("405C")
else if (colorIndicator < 139060) sendCustomCommand("405B")
else if (colorIndicator < 139100) sendCustomCommand("405A")
else if (colorIndicator < 140025) sendCustomCommand("4059")
else if (colorIndicator < 140050) sendCustomCommand("4058")
else if (colorIndicator < 140075) sendCustomCommand("4057")
else if (colorIndicator < 140100) sendCustomCommand("4056")
else if (colorIndicator < 141025) sendCustomCommand("4055")
else if (colorIndicator < 141050) sendCustomCommand("4054")
else if (colorIndicator < 141075) sendCustomCommand("4053")
else if (colorIndicator < 141100) sendCustomCommand("4052")
else if (colorIndicator < 142025) sendCustomCommand("4051")
else if (colorIndicator < 142050) sendCustomCommand("4050")
else if (colorIndicator < 142075) sendCustomCommand("404F")
else if (colorIndicator < 142100) sendCustomCommand("404E")
else if (colorIndicator < 143015) sendCustomCommand("404D")
else if (colorIndicator < 143030) sendCustomCommand("404C")
else if (colorIndicator < 143045) sendCustomCommand("404B")
else if (colorIndicator < 143060) sendCustomCommand("404A")
else if (colorIndicator < 143085) sendCustomCommand("4049")
else if (colorIndicator < 143100) sendCustomCommand("4048")
else if (colorIndicator < 144015) sendCustomCommand("4047")
else if (colorIndicator < 144030) sendCustomCommand("4046")
else if (colorIndicator < 144045) sendCustomCommand("4045")
else if (colorIndicator < 144060) sendCustomCommand("4044")
else if (colorIndicator < 144085) sendCustomCommand("4043")
else if (colorIndicator < 144100) sendCustomCommand("4042")
else if (colorIndicator < 145050) sendCustomCommand("4041")
else if (colorIndicator < 145100) sendCustomCommand("4040")
else if (colorIndicator < 146030) sendCustomCommand("403F")
else if (colorIndicator < 146060) sendCustomCommand("403E")
else if (colorIndicator < 146100) sendCustomCommand("403D")
else if (colorIndicator < 147050) sendCustomCommand("403C")
else if (colorIndicator < 147100) sendCustomCommand("403B")
else if (colorIndicator < 148030) sendCustomCommand("403A")
else if (colorIndicator < 148060) sendCustomCommand("4039")
else if (colorIndicator < 148100) sendCustomCommand("4038")
else if (colorIndicator < 149030) sendCustomCommand("4037")
else if (colorIndicator < 149060) sendCustomCommand("4036")
else if (colorIndicator < 149100) sendCustomCommand("4035")
else if (colorIndicator < 150050) sendCustomCommand("4034")
else if (colorIndicator < 150100) sendCustomCommand("4033")
else if (colorIndicator < 151030) sendCustomCommand("4032")
else if (colorIndicator < 151060) sendCustomCommand("4031")
else if (colorIndicator < 151100) sendCustomCommand("4030")
else if (colorIndicator < 152100) sendCustomCommand("402F")
else if (colorIndicator < 153050) sendCustomCommand("402E")
else if (colorIndicator < 153100) sendCustomCommand("402D")
else if (colorIndicator < 154050) sendCustomCommand("402C")
else if (colorIndicator < 154100) sendCustomCommand("402B")
else if (colorIndicator < 155050) sendCustomCommand("402A")
else if (colorIndicator < 155100) sendCustomCommand("4029")
else if (colorIndicator < 156050) sendCustomCommand("4028")
else if (colorIndicator < 156100) sendCustomCommand("4027")
else if (colorIndicator < 157050) sendCustomCommand("4026")
else if (colorIndicator < 157100) sendCustomCommand("4025")
else if (colorIndicator < 158050) sendCustomCommand("4024")
else if (colorIndicator < 158100) sendCustomCommand("4023")
else if (colorIndicator < 159050) sendCustomCommand("4022")
else if (colorIndicator < 159100) sendCustomCommand("4021")
else if (colorIndicator < 160100) sendCustomCommand("4020")
else if (colorIndicator < 161050) sendCustomCommand("401F")
else if (colorIndicator < 161100) sendCustomCommand("401E")
else if (colorIndicator < 162030) sendCustomCommand("401D")
else if (colorIndicator < 162060) sendCustomCommand("401C")
else if (colorIndicator < 162100) sendCustomCommand("401B")
else if (colorIndicator < 163050) sendCustomCommand("401A")
else if (colorIndicator < 163100) sendCustomCommand("4019")
else if (colorIndicator < 164050) sendCustomCommand("4018")
else if (colorIndicator < 164100) sendCustomCommand("4017")
else if (colorIndicator < 165050) sendCustomCommand("4016")
else if (colorIndicator < 165100) sendCustomCommand("4015")
else if (colorIndicator < 166030) sendCustomCommand("4014")
else if (colorIndicator < 166060) sendCustomCommand("4013")
else if (colorIndicator < 166100) sendCustomCommand("4012")
else if (colorIndicator < 167050) sendCustomCommand("4011")
else if (colorIndicator < 167100) sendCustomCommand("4010")
else if (colorIndicator < 168020) sendCustomCommand("400F")
else if (colorIndicator < 168040) sendCustomCommand("400E")
else if (colorIndicator < 168060) sendCustomCommand("400D")
else if (colorIndicator < 168080) sendCustomCommand("400C")
else if (colorIndicator < 168100) sendCustomCommand("400B")
else if (colorIndicator < 169015) sendCustomCommand("400A")
else if (colorIndicator < 169030) sendCustomCommand("4009")
else if (colorIndicator < 169055) sendCustomCommand("4008")
else if (colorIndicator < 169070) sendCustomCommand("4007")
else if (colorIndicator < 169085) sendCustomCommand("4006")
else if (colorIndicator < 169100) sendCustomCommand("4005")
else if (colorIndicator < 170020) sendCustomCommand("4004")
else if (colorIndicator < 170040) sendCustomCommand("4003")
else if (colorIndicator < 170060) sendCustomCommand("4002")
else if (colorIndicator < 170080) sendCustomCommand("4001")
else if (colorIndicator < 170100) sendCustomCommand("4000")
else if (colorIndicator < 171050) sendCustomCommand("40FF")
else if (colorIndicator < 171100) sendCustomCommand("40FE")
else if (colorIndicator < 172030) sendCustomCommand("40FD")
else if (colorIndicator < 172060) sendCustomCommand("40FC")
else if (colorIndicator < 172100) sendCustomCommand("40FB")
else if (colorIndicator < 173025) sendCustomCommand("40FA")
else if (colorIndicator < 173050) sendCustomCommand("40F9")
else if (colorIndicator < 173075) sendCustomCommand("40F8")
else if (colorIndicator < 173100) sendCustomCommand("40F7")
else if (colorIndicator < 174030) sendCustomCommand("40F6")
else if (colorIndicator < 174060) sendCustomCommand("40F5")
else if (colorIndicator < 174100) sendCustomCommand("40F4")
else if (colorIndicator < 175030) sendCustomCommand("40F3")
else if (colorIndicator < 175060) sendCustomCommand("40F2")
else if (colorIndicator < 175100) sendCustomCommand("40F1")
else if (colorIndicator < 176030) sendCustomCommand("40F0")
else if (colorIndicator < 176060) sendCustomCommand("40EF")
else if (colorIndicator < 176100) sendCustomCommand("40EE")
else if (colorIndicator < 177020) sendCustomCommand("40ED")
else if (colorIndicator < 177040) sendCustomCommand("40EC")
else if (colorIndicator < 177060) sendCustomCommand("40EB")
else if (colorIndicator < 177080) sendCustomCommand("40EA")
else if (colorIndicator < 177100) sendCustomCommand("40E9")
else if (colorIndicator < 178025) sendCustomCommand("40E8")
else if (colorIndicator < 178050) sendCustomCommand("40E7")
else if (colorIndicator < 178075) sendCustomCommand("40E6")
else if (colorIndicator < 178100) sendCustomCommand("40E5")
else if (colorIndicator < 179025) sendCustomCommand("40E4")
else if (colorIndicator < 179050) sendCustomCommand("40E3")
else if (colorIndicator < 179075) sendCustomCommand("40E2")
else if (colorIndicator < 179100) sendCustomCommand("40E1")
else if (colorIndicator < 180100) sendCustomCommand("40E0")
else if (colorIndicator < 181050) sendCustomCommand("40DF")
else if (colorIndicator < 181100) sendCustomCommand("40DE")
else if (colorIndicator < 182050) sendCustomCommand("40DD")
else if (colorIndicator < 182100) sendCustomCommand("40DC")
else if (colorIndicator < 183100) sendCustomCommand("40DB")
else if (colorIndicator < 184050) sendCustomCommand("40DA")
else if (colorIndicator < 184100) sendCustomCommand("40D9")
else if (colorIndicator < 185050) sendCustomCommand("40D8")
else if (colorIndicator < 185100) sendCustomCommand("40D7")
else if (colorIndicator < 186100) sendCustomCommand("40D6")
else if (colorIndicator < 187050) sendCustomCommand("40D5")
else if (colorIndicator < 187100) sendCustomCommand("40D4")
else if (colorIndicator < 188050) sendCustomCommand("40D3")
else if (colorIndicator < 188100) sendCustomCommand("40D2")
else if (colorIndicator < 189100) sendCustomCommand("40D1")
else if (colorIndicator < 190020) sendCustomCommand("40D0")
else if (colorIndicator < 190040) sendCustomCommand("40CF")
else if (colorIndicator < 190060) sendCustomCommand("40CE")
else if (colorIndicator < 190080) sendCustomCommand("40CD")
else if (colorIndicator < 190100) sendCustomCommand("40CC")
else if (colorIndicator < 191015) sendCustomCommand("40CB")
else if (colorIndicator < 191030) sendCustomCommand("40CA")
else if (colorIndicator < 191045) sendCustomCommand("40C9")
else if (colorIndicator < 191060) sendCustomCommand("40C8")
else if (colorIndicator < 191085) sendCustomCommand("40C7")
else if (colorIndicator < 191100) sendCustomCommand("40C6")
else if (colorIndicator < 192020) sendCustomCommand("40C5")
else if (colorIndicator < 192040) sendCustomCommand("40C4")
else if (colorIndicator < 192060) sendCustomCommand("40C3")
else if (colorIndicator < 192080) sendCustomCommand("40C2")
else if (colorIndicator < 192100) sendCustomCommand("40C1")
else if (colorIndicator < 193050) sendCustomCommand("40C0")
else if (colorIndicator < 193100) sendCustomCommand("40BF")
else if (colorIndicator < 194050) sendCustomCommand("40BE")
else if (colorIndicator < 194100) sendCustomCommand("40BD")
else if (colorIndicator < 195030) sendCustomCommand("40BC")
else if (colorIndicator < 195060) sendCustomCommand("40BB")
else if (colorIndicator < 195100) sendCustomCommand("40BA")
else if (colorIndicator < 196030) sendCustomCommand("40B9")
else if (colorIndicator < 196060) sendCustomCommand("40B8")
else if (colorIndicator < 196100) sendCustomCommand("40B7")
else if (colorIndicator < 197050) sendCustomCommand("40B6")
else if (colorIndicator < 197100) sendCustomCommand("40B5")
else if (colorIndicator < 198050) sendCustomCommand("40B4")
else if (colorIndicator < 198100) sendCustomCommand("40B3")
else if (colorIndicator < 198030) sendCustomCommand("40B2")
else if (colorIndicator < 199060) sendCustomCommand("40B1")
else if (colorIndicator < 199100) sendCustomCommand("40B0")
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.