Solved

Definition of Color Value

Posted on 2010-08-14
8
622 Views
Last Modified: 2013-12-17
I have a .Net (C#) application that requires a color value in what appears to be a base 10 format but which must be preceeded by a minus sign.  For example: -16776961 represents a standard blue color.  My questions are: what color standard is this?  Why would this format - whatever it is - be used instead of defining the color using the hex value?
0
Comment
Question by:dev775
8 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 33437887
what the application do?
what is the usage of the colors in the application?
0
 

Author Comment

by:dev775
ID: 33437910
This is a database application.  The color values set the color of a row in a gridcontrol.  Thanks for your help!
0
 
LVL 5

Expert Comment

by:KiasChaos83
ID: 33438437
This value you provided does actually convert to an RGB value.

Explanation:

Firstly, an RGB colour is made up of 3 values - one for red, green and blue. Typically you would use a byte for each value (8 bits). So to store 3 bytes worth of data you would need a 32 bit int (a 16 bit int could only hold 2 bytes).  But a 32 bit int has 4 bytes and for an RGB you only need 3 bytes so the first byte would be meaningless. The second byte would represent red, the third would represent green and the fourth would be blue.

If you take a look at what -16776961 converts to in binary (i.e. in to a 32 bits) you'll see how this fits in. See http://www.binaryconvert.com/result_signed_int.html?decimal=045049054055055054057054049

Now, for the record a hex colour value is the same as an RGB value. Hex means base16, so to represent a hex value you only need 4 bits (4^2=16). But, for example, the color blue is represented by 1 byte (8 bits), so you would use 2 hex values to represent the byte. How would you represent the blue colour? You would use #0000FF, where the first two hex values represent the 8 bits for red, the second two hex values represent the 8 bit value for green, and so on.

Hope that helps.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:dev775
ID: 33438899
Kias - this is a very helpful explanation - thank you.  I am a little confused with the conversion from hex to decimal.  For example if I have a hex value = 00FF00 (which I think should be green), then how do I get to the decimal value with the minus sign?  The utility does not seem to produce the correct result. Thanks again.
0
 
LVL 3

Expert Comment

by:bkokx
ID: 33439543
A complete color code is not only the RGB value but ARGB, it is preceeded by Alpha, or transparency if you like. See http://msdn.microsoft.com/en-us/library/system.drawing.color.aspx.

In hex the color would be AARRGGBB where the AA is usually FF for non-transparent so a full green color would become FF00FF00 which will result in a negative value for a 32 bit integer.
0
 
LVL 5

Accepted Solution

by:
KiasChaos83 earned 500 total points
ID: 33439596
Hi dev775,

You really need to think in bits and bytes. A 32 bit integer contains exactly 32 bits.. it might look something like this

00000000000000000000000000000000

or this

11111111111111111111111111111111

or this

00101001011110111011100101001011

They are what a 32bit int might look like in binary. But in decimal value the range of a 32bit integer will be between -2147483648 to 2147483647.

Let's take the colour value  -16776961 for example. It has a binary value of

11111111000000000000000011111111

Now, we know that since there are 8 bits in a byte, there are 4 bytes in a 32bit integer. I'll separate the bites with an underline _

11111111_00000000_00000000_11111111

As bkokx explained, if we take a 32bit integer to represent ARGB then we know that

Alpha = 11111111
Red = 00000000
Green = 00000000
Blue = 11111111

Now, hex is base 16. That means that there are 16 different digits available. They are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. To store any one of those values it will take 4 bits. Since there are 8 bits in a byte, we need to represent the 8 bits using two different hex digits.

So, let's separate the bits using underscores so we can work out the hex values

Alpha = 1111_1111
Red = 0000_0000
Green = 0000_0000
Blue = 1111_1111

Let's swap the 4 bit pieces with their equivalent hex value

Alpha = F_F
Red = 0_0
Green = 0_0
Blue = F_F

If we join them all back together without the underscores we get

FF0000FF

The first two digits represent the alpha. If we're only worried about RGB and not ARGB, then we just ignore them. So the RGB value is

0000FF.

If that doesn't make sense there here are some references to learn the relationship between binary, hex, decimals and ints.

http://www.swarthmore.edu/NatSci/echeeve1/Ref/BinaryMath/NumSys.html
http://en.wikipedia.org/wiki/Binary_numeral_system
http://msdn.microsoft.com/en-us/library/system.int32.aspx



0
 
LVL 5

Expert Comment

by:KiasChaos83
ID: 33439601
You probably need to learn about 2's compliment too..

http://en.wikipedia.org/wiki/Two's_complement
0
 

Author Closing Comment

by:dev775
ID: 33442229
This was an excellent and very thorough response!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question