Main Menu

Point(x,y)

Started by Chas, December 11, 2005, 12:38:33 PM

Previous topic - Next topic

Chas

Hi,
Ive been trying to get the colour of a pixel, but I cant understand the value that Point()  returns eg:

; Clear the Screen to a grey colour.
 Cls RGB(192, 192, 192)  

 
; Display the colour value
 Print Point(100,100)

 Sync
 WaitKey

It doesnt seem to be RGB??

kevin

#1
are you running 32bit ?.. if your not, then the colours rgb's will be masked to 16bit. Since everything is stored in the same bit depth as the screen.

Chas

are you running 32bit ?..

Yes I reckon so; (project, settings,screen.. 32) ?

kevin

#3
The original example is correct.  What are you expecting the result to be ?




PlayBASIC Code: [Select]
c=RGB(192, 192, 192)

Cls c

; display colour
print c

; Display the colour value
Print Point(100,100)


sync
waitkey



Chas

#4
Hmmm
I was expecting 192192192
-Apologies I should have read the help file!
Thanks
Chas

kevin

ahh,  now i'm with ya!

Colours for us are really just represented as integer (decimal) values.   These values will range between 0 and 2^24 (24 million).  This gives us 24 million unique colours

All colours resolved down to a integer number for ease, where the R ,G , B levels are mixed together to create this value.    Where each R, G ,B  level will range between 0 and 255.

The confusing apart about this is that internally computers don't use base 10 numbers systems like us  (integers).  They actually store information in binary form.  This is what is causing the unexpected result here.

Binary is a base 2 number system.   Where each each digit has only two states, one or zero. (on or off)

Binary numbers are presented with a "%"  prefix.  You can represent any decimal in binary and vice versa.  

unlike decimal where each column is a multiple of ten (base 10 number system) . In Binary it's a mult of 2.  So the for a 16 digit binary number places (it goes up 32 places) are as follows.
   


32768, 16384, 8192, 4096, 2048, 1024, 256, 128, 64, 32, 16, 8, 4, 2, 1


Some 4 digit binary examples  (numbers from 0 to 15)



bin    = combined bin columns  =decimal

%0000  =    =0
%0001  = 1    =1
%0010  = 2    =2
%0101  = 2+1   =3
%0100  = 4   =4
%0101  = 4+1   =5
%0110  = 4+2   =6
%0111  = 4+2+1   =7
%1000  = 8   =8
%1001  = 8+1   =9
%1010  = 8+2   =10
%1011  = 8+2+1   =11
%1100  = 8+4   =12
%1101  = 8+4+1   =13
%1110  = 8+4+2   =14
%1111  = 8+4+2+1  =15



Some 8 digit binary examples  (representing numbers from 0 to 255)


bin        = combined bin columns  =decimal

%00000000  = 0    =0
%00000001  = 1    =1
%00001111  = 8+4+2+1   =15    
%00010000  = 16    =16
%00100001  = 32+1   =33
%01100000  = 64+32   =96
%10000000  = 128   =128
%11000000  = 128+64      =192
%11000000  = 128+64+1     =193
%11000000  = 128+64+32+16+8+4+2+1 =255








What you should notice is that each grouping of 8 binary places gives us the numeric range between 0 and 255, just like our colours even been using !.  

  So therefore to represent 3 colours side by side in binary form,  would take a 24 binary digits  (8 + 8 + 8)...

 example.
 %000000000000000000000000

 This means each group of 8 digits corresponds to either a R,G,B  level.   So by change a binary digit within a certain range will effect the level of this colour component.

  R ='s digits 24-> 17
  G ='s digits 16-> 9
  B ='s digits 8-> 1




RRRRRRRRGGGGGGGGBBBBBBBB    
%000000000000000000000000


Examples



; Blue
;                     rrrrrrrrggggggggbbbbbbbb
circlec 100,100,25,1,%000000000000000011111111

; Green
;                     rrrrrrrrggggggggbbbbbbbb
circlec 150,100,25,1,%000000001111111100000000

; Red
;                     rrrrrrrrggggggggbbbbbbbb
circlec 200,100,25,1,%111111110000000000000000


; purple
;                     rrrrrrrrggggggggbbbbbbbb
circlec 200,100,25,1,%111111110000000011111111


; yellow
;                     rrrrrrrrggggggggbbbbbbbb
circlec 250,100,25,1,%111111111111111100000000

; white
;                     rrrrrrrrggggggggbbbbbbbb
circlec 300,100,25,1,%111111111111111111111111


;grey
;                     rrrrrrrrggggggggbbbbbbbb
circlec 350,100,25,1,%110000001100000011000000


sync
waitkey





Now obviously working in binary would be a royal pain, with so many digits,  but we could also use HEX.   Hexadecimal is a base 16 number system.  And an equally common number system used when programming.

Hex numbers have a $ symbol prefix.   In Base 16 each digit has a range 0-15.   The values 10,11,12,13,14,15 are represented alphabetically.    


dec= hex

0=0
1=1
2=2
3=3
4=4
5=5
6=6
7=7
8=8
9=9
10=A
11=B
12=C
13=D
14=E
15=F




Each digit colun in a hex number is   is worth

 4096,  256,  16 , 1


Each digit in a hex number can represent the same numeric range as a 4digit binary number (0 to 15).   So we can use hex to represent colours also.    

This time, each pair of hex digits presents a  R,G, B channel.   So to represent a complete RGB colour we need a 6 digit hex value.

R= digits   6 and 5
G= digits   4 and 3
B= digits   2 and 1


Here are some 4 digit examples


Hex       = combined Hex columns   =decimal

$0000   = ( 0*4096)+(0*256)+(0*16)+(0*1) =0
$0001   = ( 0*4096)+(0*256)+(0*16)+(1*1) =1
$0002   = ( 0*4096)+(0*256)+(0*16)+(2*1) =2
$0003   = ( 0*4096)+(0*256)+(0*16)+(3*1) =3
$0004   = ( 0*4096)+(0*256)+(0*16)+(4*1) =4
$0005   = ( 0*4096)+(0*256)+(0*16)+(5*1) =5
$0006   = ( 0*4096)+(0*256)+(0*16)+(6*1) =6
$0007   = ( 0*4096)+(0*256)+(0*16)+(7*1) =7
$0008   = ( 0*4096)+(0*256)+(0*16)+(8*1) =8
$0009   = ( 0*4096)+(0*256)+(0*16)+(9*1) =9
$000a   = ( 0*4096)+(0*256)+(0*16)+(10*1) =10
$000b   = ( 0*4096)+(0*256)+(0*16)+(11*1) =11
$000c   = ( 0*4096)+(0*256)+(0*16)+(12*1) =12
$000d   = ( 0*4096)+(0*256)+(0*16)+(13*1) =13
$000e   = ( 0*4096)+(0*256)+(0*16)+(14*1) =14
$000f   = ( 0*4096)+(0*256)+(0*16)+(15*1) =15

; Misc examples

$00ff   = ( 0*4096)+(0*256)+(15*16)+(15*1) =255
$0100   = ( 0*4096)+(1*256)+(0*16)+(0*1) =256
$0201   = ( 0*4096)+(2*256)+(0*16)+(1*1) =513
$100a   = ( 1*4096)+(0*256)+(0*16)+(10*1) =4106
$aaaa   = (10*4096)+(10*256)+(10*16)+(10*1) =43690
$ffff   = ( 15*4096)+(15*256)+(15*16)+(15*1) =65535




Example



; Blue
;                     rrggbb
circlec 100,100,25,1,$0000fff

; Green
;                     rrggbb
circlec 150,100,25,1,$00ff00

; Red
;                     rrggbb
circlec 200,100,25,1,$ff0000


; purple
;                     rrggbb
circlec 200,100,25,1,$ff00ff

; yellow
;                     rrggbb
circlec 250,100,25,1,$ffff00

; white
;                     rrggbb
circlec 300,100,25,1,$ffffff


;grey
;                     rrggbb
circlec 350,100,25,1,$c0c0c0


sync
waitkey




Once your familiar with hex it's a very useful (and quick) way to represent not only colours, but very large numbers when programming.  Hex and Binary are completary also.

Anyway, getting back to the point of this post.  Is that the RGB command is really just a helper function for converting the separate R,G, B colours levels, into a colour value.  The resulting value will not be a decimal form, but a binary form.

So if we read a colour and display it as an integer.  It generally won't make a lot of sense to us.  

Now If you want to retrieve the R/G/B level from a colour value, you can use the RGBR(), RGBG(), RGBB() functions.  





Cls rgb(100,50,25)

c=point(100,100)
print c
print bin$(c)
print hex$(c)
print rgbr(c)
print rgbg(c)
print rgbb(c)

sync
waitkey





Phew...  I've run out of time.  but i hope this has given you something to chew on.

Chas

Another illuminating explantion :)
-I have even glimpsed some of the light re. Hexadecimal, which left me cold before.

This thread should really be moved to Beginners -Its my fault completely.
Thank you for all your time and help -Chas

kevin

Ok since it's not a bug i'll move this to the beginners forum.