Here's one for the brainpans amongst us!
When coding in Visual Basic 5, trying to create a simple draw
program, I came across the methods Point and PSet.
Point has the syntax OBJECT.Point (X,Y) and this returns the colour
at point (X,Y) as a single integer.
PSet has the syntax OBJECT.PSet (X,Y), RGB(R, G, B) and this sets
the colour at point (X,Y) using the RGB syntax.
In the PSet method the variables R, G, & B are all integers
ranging form 0 to 255 and the value(V) returned in the Point method
is an integer ranging from 0 (black) to 16777215 (white).
I was wondering if I could find the relationship, so after a while
of trile and error I found that V = R + (256) G + (2562)
B.
I also realised that R, G, and B could be worked out by a series of
calculations given only V.
I put it to you to develop a proof for this. Good luck.
Yours sincerely
no. p385 (James)
This seems like more of a computer science
question than a maths one. I believe that your information is not
quite correct, and here's the full story:
Programmers generally use hexadecimal numbers and powers of two,
since they store 0s and 1s (ie 2 states). Note that
256=28. If you haven't seen hexadecimal notation, it
works like this:
1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,...
This means that you don't get to 10 after 9. There are six more
'numbers' inbetween, representing 11 to 15. To avoid confusion, we
shall write an x in front of a hexadecimal number. Then, for
example, x6B2 should be read as
x2+xB×16+x6×(16×16) = 2+176+1536 =1714. It's a
bit confusing at first, but makes programming a lot easier.
So, how does VB store colours? Each of RGB can go from 0 to 255,
which is x0 to xFF. To create a large number which has all of the
information in it, just put the three numbers together. For
example, to have R=x55, G=x7F, B=x00 we would write
V=x557F00
Looking at the value of V in hexadecimal, we can immediately tell
how much red, green and blue is in it.
If you understood how to calculate hexadecimal numbers, you might
have noticed that the above can be rephrased:
V=R×(2562)+G×256+B
To get R,G,B from V we convert to hex like the above, and one way
of doing this is the following:
Divide V by (2562) and look at the number before the
decimal point. This is R.
Divide (V-R×(2562)) by 256 and again look at the
number before the decimal point. This is G.
Finally, B=V-R×(2562)-G×256.
This only works because each of R,G,B is less than 256. Without
this restriction, we could not recover them like this. Can you see
why?
Many calculators/computer programs will convert numbers between
decimal (normal) and hexadecimal. You might want to see what Octal
and binary are like too.
Finally, the largest number you can have, which corresponds to pure
white, is xFFFFFF=16777215, which makes more sense when it's in
hexadecimal format.
Best of luck with your programming.
-Dave