-3

I am learning Arduino. Now I want to change the color of the background.

I am using the Uno Board, and the Sparkfun Color LCD Shield. I am also using the SparkFunColorLCDShield Library.

The easiest way the change the color is to do lcd.clean(HEXCODE); Example for white background: lcd.clean(0xFFF); (12Bit of 0xFFFFFF) It's also working with lcd.clean(16777215);

The 16777215 is the decimal number of 0xFFFFFF. Now I need a method, that gives me the decimal number of any HEX Number.

More examples:

  • 00FFFFF -> 1048575
  • 000FFFF -> 65535
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Bundeskanzler.
  • 90
  • 1
  • 11
  • It's unclear what you're asking, please elaborate your question. Especially define "HEX of that." And how could you save "FFFFFF" in a byte (!) ?? – Jabberwocky Jan 31 '17 at 15:12
  • 3
    To begin with, there's no way to store anything that could be represented by "FFFFFF" in a byte, so that's contradictory. Also, "FFFFFF" already looks hexadecimal, so it's unclear what you mean by converting it to hexadecimal. It also isn't clear what is supposed to be 12 bits. Finally, I've no idea how the code in the question relates to the question, as all it does is read serial data, which isn't mentioned anywhere else in the question. – Dolda2000 Jan 31 '17 at 15:25
  • I would assume that by FFFFFF, you mean a RGB presentation of R=0xFF, G=0xFF, B=0xFF? This is probably what needs to be clarified, especially since this isn't standardized - there's RGB and RBG. – Lundin Jan 31 '17 at 15:25
  • Have the same problem, but with 16 bits instead of 12... I can post my solution if you make a clearer question. – Luiz Menezes Jan 31 '17 at 15:26
  • @Bundeskanzler. OK, it's getting somewhat clearer, maybe you just should put some examples if you can't express it in English, for example: I have a string "ff" and I want to transform it into the number 255. – Jabberwocky Jan 31 '17 at 15:32
  • 1
    @Dolda2000 this question is a followup of [this discussion](http://stackoverflow.com/questions/41952838/arduino-clean-lcd-shield-with-color-codes), that should clear things up – Patrick Trentin Jan 31 '17 at 15:33
  • I have absolut no idea how I can make it clearer :/ I want to have a colored background on my Sparkfun Shield. The Library just accept HEX codes. And I must convert my normal color code, which is used in html for example, to a hex color code – Bundeskanzler. Jan 31 '17 at 15:34
  • @Bundeskanzler. Sorry I can't see the logic in this transformation : 00ffff -> 0FF. Please give us some more examples, what should following codes be transformed to: 00aabb, aa00bb, 0000ee, and please put that into the question. – Jabberwocky Jan 31 '17 at 15:39
  • @Bundeskanzler: PatrickTrentin's comment alone clears up a great deal. You're just using a lot of abstractions and assumptions that aren't shared by the rest of us. It would really help if you described your setup and the LCD you're using in the question. Also, if you could link to the documentation for the functions you're using, you may be able to get help from people (like me) who know C but aren't using the hardware in question. Further, please describe more precisely what you want to do with the serial input (what the expected format is, &c.). – Dolda2000 Jan 31 '17 at 15:39
  • 2
    @MichaelWalz: I suspect he wants to convert 24-bit RGB codes to twelve bit codes by discarding the lower nibble of each component. Probably, he wants to read 24-bit RGB codes as strings over the serial port and convert them to `int`-encoded twelve-bit codes. That's still just an assumption, though. – Dolda2000 Jan 31 '17 at 15:40
  • Just a quick note, every two 'FF's is one byte so e.g. `0xFFFFFF` or `0xABCDEF` is 3 bytes. – RastaJedi Jan 31 '17 at 15:47
  • I added an *answer* that I hope could clarify the question, however keep in mind according to @Bundeskanzler. it does not work, and I cannot test it. – Patrick Trentin Jan 31 '17 at 15:47
  • I think you are right @Dolda; OP, is [this](http://stackoverflow.com/questions/3108860/how-to-use-3-digit-color-codes-rather-than-6-digit-color-codes-in-css) the 12-bit version you are referring to? If you read through that, you will see that shorthand doesn't work for every color. – RastaJedi Jan 31 '17 at 16:02
  • Well, after reading the interface tutorial, it looks like it only has 12-but color definition, so the 12-bit version will work for every color the LCD can display, but not every color you normally can display with HTML 24-bit color ("12-bit color rendition (4 bits red, 4-bits green, 4-bits blue)"). – RastaJedi Jan 31 '17 at 16:19
  • @RastaJedi please write in proper English, there are several non-native English speakers on this board. – Patrick Trentin Jan 31 '17 at 16:22
  • @Patrick, broken keyboard, had to get on phone to edit, sorry. – RastaJedi Jan 31 '17 at 16:23

1 Answers1

1

After much guessing and reading here:

Maybe you want this:

#include <stdlib.h>
#include <stdio.h>


int Convert24bitColorStringTo12BitColor(const char *colorcode24)
{
  long color24 = strtol(colorcode24, NULL, 16);
  long color12 = ((color24 & 0xf0) >> 4) | ((color24 & 0xf000) >> 8) | ((color24 & 0xf00000) >> 12);
  return color12;
}

int main()
{
  printf("%x\n", Convert24bitColorStringTo12BitColor("abcdef"));
  printf("%x\n", Convert24bitColorStringTo12BitColor("ffff"));
}

This prints:

ace
ff
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115