inverse hex codes

Discussion in 'Programmer's Corner' started by Bear_2759, Feb 11, 2012.

  1. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    hey guys, so I bought myself an arduino uno R3 and a nokia 6100 LCD shield to play with and learn a bit about programming. first thing I've had to do though and almost done, is re-write the driver lol. for the most part it's done, but now I need to know if anyone can tell me a site that'll have inverted 12bit hex colour codes. I've been searching but cant find anything. or is there an easy way to invert?

    something like this http://rangevoting.org/ColorCode.html
    but inverted. it took me a while to clue into it and decided to swap black and white in the driver and now they work fine. so have to invert all the other colours that're declared there and also any other colours set by hex code elsewhere :(
     
  2. codehead

    Member

    Nov 28, 2011
    56
    11
    Sorry if I'm misunderstanding—but you just need to invert the bits, right? Hex 000 becomes FFF, etc? One's complement in C is the tilde character (~).
     
  3. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    that worked, thanks. now that some of the colours are appearing correctly on screen, I've got another issue. the only colours that are apparing correctly are pink, red, white and black. everything else appears as one of those 4. I'm tempted to re-write the whole driver but for a noob this would be a big job.
     
  4. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    getting closer, re-writing the drivers wasn't quite as complex as I thought it would be since I had a refrence driver to go off. I did find an inversion command that was switched on, turning this off half solved my issue.

    now some colours are appearing correctly, but reds and blues appear to be swapped. for example, take the last table on this page http://www.december.com/html/spec/color3hex4.html
    F00 through to F0F. if I just have a whole lot of LCDClear(colour) commands one after the other so it scales through them all, it starts as BLUE instead of red and ends up at fuchsia. same if I went with one of the tables that had blue in it, it'd start at red.

    any ideas? I think I'm going to be stuck with this one, and if I dont find an answer I'll be stuck with experimenting with hex codes till I find the colours I want. :confused:
     
  5. codehead

    Member

    Nov 28, 2011
    56
    11
    So it seems you need to swap the first and third nibble? In C, try something like...

    Code ( (Unknown Language)):
    1.  
    2.   colorVal = ((colorVal & 0x0f) << 8) | (colorVal & 0xf0) | ((colorVal >> 8) & 0x0f);
    3.  
     
  6. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    thanks for that, I'll give it a go when I get home. so I understand it better, and am able to experiment can you explain what it does a bit?
     
  7. codehead

    Member

    Nov 28, 2011
    56
    11
    Sure. Say that the variable colorVal holds (hexadecimal) 0ABC. Let's break the code into three parts:

    First, "((colorVal & 0x0f) << 8)": this says take 0ABC and "&" (bit-wise AND) it with 000f, which results in masking off everything except the lower 4-bit nibble, resulting in 000C, then shift it left 8 bits to end up with 0C00.

    Second, "(colorVal & 0xf0)" masks that same value (0ABC) with 00f0 to yield 00B0.

    Third, "((colorVal >> 8) & 0x0f)" shifts 0ABC right 8 bits to yield 000A (at this point it's likely we don't need to mask it with "& 0X0f", but it doesn't hurt, and I included it in case the high nibble isn't zero—I don't know for certain what you're starting with).

    Those three sections are combined with the "|" (bit-wise OR) operators—0C00 | 00B0 | 0A00 yields 0CBA. ("+" would work fine too.)
     
  8. Bear_2759

    Thread Starter Active Member

    May 23, 2008
    120
    0
    excellent, thankyou :D.
    I added the code to one of the driver files and it's working. to make sure I created a small program that will scale through all colours and it's working well. all colours appear as they should :D
    now that I've managed to get it working, onto the real work lol.
     
Loading...