-2

I want to write code in C# to build table that converts each UNICODE code to RGB color value. I use UTF-16. this table has three columns one for char or symbol or digit, another column for it's UNICODE code and last column for one of the 16 million colors Can anyone suggest an easy way to do this???

  • 7
    UNICODE character to RGB color? What? –  Apr 21 '14 at 16:50
  • Encoding.Unicode.GetBytes returns an array of bytes, which you can use to turn into RGB however you want. However, RGB color codes have 3 bytes, while UTF-16 has anywhere from 1 to 8 bytes, which means you'll have a bad time trying to map 1 to 1. – Hans Z Apr 21 '14 at 16:51
  • 2
    There is no obvious relation between character codes and colors. What is it that you are trying to do? – Guffa Apr 21 '14 at 16:53
  • @HansZ: I think that you are confusing UTF-8 and UTF-16. An UTF-16 code is never one byte. – Guffa Apr 21 '14 at 16:54
  • Can you give some sample input and expected output? Your question doesn't makes much sense. – Sriram Sakthivel Apr 21 '14 at 16:56
  • @Guffa you're right, I was thinking of UTF-8. Regardless, Encoding.Unicode will return 1-8 bytes for GetBytes on a single character. – Hans Z Apr 21 '14 at 16:56
  • for example if "A" in UNICODE is U+0041 I want to choose any color and take it's value in binary. – Eman Bany salameh Apr 21 '14 at 17:35
  • I need this step for cryptographic algorithm – Eman Bany salameh Apr 21 '14 at 17:36
  • @HansZ: The `Encoding.Unicode` property returns the UTF-16 encoding, you use `Encodung.UTF8` to get the UTF-8 encoding. – Guffa Apr 21 '14 at 17:53
  • @EmanBanysalameh Cryptographic algorithm? Using colors? Writing your own cryptography? http://security.stackexchange.com/questions/18197/why-shouldnt-we-roll-our-own – Hans Z Apr 21 '14 at 18:10

1 Answers1

2

If you ignore combining characters and such, then each Unicode code point is 16 bits. RGB is a 24-bit space that uses one byte for red, one byte for green, and one byte for blue. Obviously you can't do that with a 16-bit code. So what you do is use, for example 5 bits for red, 5 bits for green, and 6 bits for blue. Here's one way to do it.

Say you're given a string:

const string foo = "Hello, world.";

// rrrr rggg ggbb bbbb

const int RedMask = 0xF8;  
const int GreenMask = 0xF8;
const int BlueMask = 0xFC;
const int RedShift = 8;
const int GreenShift = 3;
const int BlueShift = 2;

foreach (var c in foo)
{
    int val = c;
    int r = (val >> RedShift) & RedMask;
    int g = (val >> GreenShift) & GreenMask;
    int b = (val << BlueShift) & BlueMask;
    // now create a color
    Color clr = Color.FromArgb(r, g, b);
    // and do something with it.
}

This code uses the high 5 bits (6 bits for blue) for each color, which will give a bit more differentiation than if you use the low bits.

Nothing forces you to use 5,5,6 as I did. You could make it 5,6,5 or 6,5,5, or 3,8,5, or whatever combination of r/g/b you want. All you have to do is change the shifts and offsets.

That said, for typical English text, the colors will be very similar. You can do other interesting things, although they'll take a bit more work. For example, you could make the blue component be a combination of bits 0, 3, 6, 9, 12, and 15. Green would be bits 1, 4, 7, 10, and 13. Red would be bits 2, 5, 8, 11, and 14. That would differentiate characters that are very close together.

The idea, though, remains the same: pick specific bits from the Unicode value and use them to construct the red, green, and blue components.

Jim Mischel
  • 122,159
  • 16
  • 161
  • 305
  • Thank you for your suggestion but it's not required to convert it to 16-bits. I have no problem to take it as 24-bits.What I need is build a table like this – Eman Bany salameh Apr 21 '14 at 20:09
  • What I need is building a table in each row it has a column for char or symbol or digit, another column for it's UNICODE code and last column for one of the 16 million colors – Eman Bany salameh Apr 21 '14 at 20:19
  • @EmanBanysalameh: The problem is that Unicode code points are 16 bits, but RGB colors are 24 bits. So you have to *expand* the bits in the Unicode code point. Otherwise, if you take the low byte for R and the high byte for G, then you have no B. With the code I showed above, you could easily create a unique RGB value for each of the 65,536 single-word Unicode code points. – Jim Mischel Apr 22 '14 at 02:19
  • If you want to cover [every possible Unicode character](http://stackoverflow.com/questions/5924105/how-many-characters-can-be-mapped-with-unicode), you'll have to change the mapping somewhat. That becomes a lot more complicated. – Jim Mischel Apr 22 '14 at 02:23
  • yes, this is what I want to do (cover every possible Unicode character) – Eman Bany salameh Apr 25 '14 at 14:00
  • @EmanBanysalameh: If you want to cover every possible Unicode character, then you need to understand how UTF-16 is encoded so that you can map the bits. My understanding of Unicode encoding isn't sufficient to provide a detailed example. – Jim Mischel Apr 25 '14 at 15:19
  • what if I put all colours in a file and all UNICODE symbols in another file then read them one by one and put the UNICODE symbol in a third file and it's associated colour – Eman Bany salameh Apr 25 '14 at 15:53
  • @EmanBanysalameh: That works. – Jim Mischel Apr 25 '14 at 15:56