Posts: 799
Threads: 140
Joined: Apr 2022
Reputation:
33
10-09-2025, 07:15 AM
(This post was last modified: 10-09-2025, 07:16 AM by PhilOfPerth.)
I need to identify and use the colour of a pixel on my screen. I have written the code as attached, which tells me the RGB values, but I can't work out how to apply this to another area. I'm sure it's something simple but so am I. Any help appreciated  .
Code: (Select All) SW = 1040: SH = 750
Screen _NewImage(SW, SH, 32)
SetFont: F& = _LoadFont("C:\WINDOWS\fonts\courbd.ttf", 20, "monospace"): _Font F&
Common Shared mx, my, n, CPR, Ctr
CPR = Int(SW / _PrintWidth("X")): Ctr = Int((CPR + 1) / 2) ' CPR is chars Per Row, Ctr is horiz centre in this screensize
Line (100, 100)-(150, 150), _RGB(255, 255, 0), BF
column = 120: row = 120
pixclr = Point(column, row)
redVal = _Red(pixclr)
greenVal = _Green(pixclr)
blueVal = _Blue(pixclr)
white: Locate 3, 1: Print "RGB is "; Str$(redVal); ","; Str$(greenVal); ","; Str$(blueVal): Sleep
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) 
Please visit my Website at: http://oldendayskids.blogspot.com/
Posts: 243
Threads: 15
Joined: Apr 2024
Reputation:
30
(10-09-2025, 07:15 AM)PhilOfPerth Wrote: I need to identify and use the colour of a pixel on my screen. I have written the code as attached, which tells me the RGB values, but I can't work out how to apply this to another area. I'm sure it's something simple but so am I. Any help appreciated .
Code: (Select All) SW = 1040: SH = 750
Screen _NewImage(SW, SH, 32)
SetFont: F& = _LoadFont("C:\WINDOWS\fonts\courbd.ttf", 20, "monospace"): _Font F&
Common Shared mx, my, n, CPR, Ctr
CPR = Int(SW / _PrintWidth("X")): Ctr = Int((CPR + 1) / 2) ' CPR is chars Per Row, Ctr is horiz centre in this screensize
Line (100, 100)-(150, 150), _RGB(255, 255, 0), BF
column = 120: row = 120
pixclr = Point(column, row)
redVal = _Red(pixclr)
greenVal = _Green(pixclr)
blueVal = _Blue(pixclr)
white: Locate 3, 1: Print "RGB is "; Str$(redVal); ","; Str$(greenVal); ","; Str$(blueVal): Sleep
When you say " Apply to another Area" . are you asking how to set another Pixel to this color ? If so then the easiest way would be PSET.
Posts: 799
Threads: 140
Joined: Apr 2022
Reputation:
33
(10-09-2025, 07:20 AM)ahenry3068 Wrote: (10-09-2025, 07:15 AM)PhilOfPerth Wrote: I need to identify and use the colour of a pixel on my screen. I have written the code as attached, which tells me the RGB values, but I can't work out how to apply this to another area. I'm sure it's something simple but so am I. Any help appreciated .
Code: (Select All) SW = 1040: SH = 750
Screen _NewImage(SW, SH, 32)
SetFont: F& = _LoadFont("C:\WINDOWS\fonts\courbd.ttf", 20, "monospace"): _Font F&
Common Shared mx, my, n, CPR, Ctr
CPR = Int(SW / _PrintWidth("X")): Ctr = Int((CPR + 1) / 2) ' CPR is chars Per Row, Ctr is horiz centre in this screensize
Line (100, 100)-(150, 150), _RGB(255, 255, 0), BF
column = 120: row = 120
pixclr = Point(column, row)
redVal = _Red(pixclr)
greenVal = _Green(pixclr)
blueVal = _Blue(pixclr)
white: Locate 3, 1: Print "RGB is "; Str$(redVal); ","; Str$(greenVal); ","; Str$(blueVal): Sleep
When you say "Apply to another Area" . are you asking how to set another Pixel to this color ? If so then the easiest way would be PSET.
Thanks ahenry. Yes, that's what I want to do, I just can't find the syntax for doing it.
Something like PSet(400,400), ThisColor maybe, with ThisColor set as a Long integer? (that doesn't work though)..
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) 
Please visit my Website at: http://oldendayskids.blogspot.com/
Posts: 3,447
Threads: 376
Joined: Apr 2022
Reputation:
345
Code: (Select All)
Dim pixclr As _Unsigned Long '<--- This is essential
SW = 1040: SH = 750
Screen _NewImage(SW, SH, 32)
SetFont: F& = _LoadFont("C:\WINDOWS\fonts\courbd.ttf", 20, "monospace"): _Font F&
Common Shared mx, my, n, CPR, Ctr
CPR = Int(SW / _PrintWidth("X")): Ctr = Int((CPR + 1) / 2) ' CPR is chars Per Row, Ctr is horiz centre in this screensize
Line (100, 100)-(150, 150), _RGB(255, 255, 100), BF
column = 120: row = 120
pixclr = Point(column, row)
redVal = _Red(pixclr)
greenVal = _Green(pixclr)
blueVal = _Blue(pixclr)
white: Locate 3, 1: Print "RGB is "; Str$(redVal); ","; Str$(greenVal); ","; Str$(blueVal): Sleep
'<---- Looks what happens when we have an undefined variable and it defaults to SINGLE TYPE
pixclr_undefined = Point(column, row)
redVal = _Red(pixclr_undefined)
greenVal = _Green(pixclr_undefined)
blueVal = _Blue(pixclr_undefined)
Locate 4, 1: Print "RGB is "; Str$(redVal); ","; Str$(greenVal); ","; Str$(blueVal): Sleep
'<--- The above is a PRECISION error. SINGLE variable type doesn't hold all the BLUE values for us, as shown here.
'and to put the color elsewhere:
For y = 0 To 749
PSet (500, y), _RGB32(redVal, greenVal, blueVal)
Next
Sleep
Posts: 3,447
Threads: 376
Joined: Apr 2022
Reputation:
345
10-09-2025, 07:50 AM
(This post was last modified: 10-09-2025, 07:51 AM by SMcNeill.)
https://qb64phoenix.com/qb64wiki/index.php/RGB32 <-- That's the command you're looking for.
*But also look close at the comments about variable type and run the demo above to see what I'm talking about.*
Posts: 799
Threads: 140
Joined: Apr 2022
Reputation:
33
10-09-2025, 08:44 AM
(This post was last modified: 10-09-2025, 10:38 AM by PhilOfPerth.)
Thanks Steve.
I struggled to understand that - more a function of my ability than the quality of the examples I'm afraid - but finally came up with thiis:
Code: (Select All) SW = 1040: SH = 750
Screen _NewImage(SW, SH, 32)
'SetFont: F& = _LoadFont("C:\WINDOWS\fonts\courbd.ttf", 20, "monospace"): _Font F&
Line (100, 100)-(150, 150), _RGB(255, 255, 0), BF ' yellow square
Color _RGB(255, 255, 255) ' change to white (to ensure no cheating)
Print "White"
H = 120: V = 120 ' select point inside square
pixclr = Point(H, V) ' identify colour of point
Color pixclr ' change to this colour
PSet (200, 200)
Draw "r100d100l100u100" ' draw square in this colour
Quite simple, I guess. But how do I use this in a Color _RGB(r,g.b), _RGB(r,g,b) format? It doesn't allow _RGB(r,g,b),(pixclr)
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, Western Australia.) 
Please visit my Website at: http://oldendayskids.blogspot.com/
Posts: 3,447
Threads: 376
Joined: Apr 2022
Reputation:
345
(10-09-2025, 08:44 AM)PhilOfPerth Wrote: Thanks Steve.
I struggled to understand that - more a function of my ability than the quality of the examples I'm afraid - but finally came up with thiis:
Code: (Select All) SW = 1040: SH = 750
Screen _NewImage(SW, SH, 32)
'SetFont: F& = _LoadFont("C:\WINDOWS\fonts\courbd.ttf", 20, "monospace"): _Font F&
Line (100, 100)-(150, 150), _RGB(255, 255, 0), BF ' yellow square
Color _RGB(255, 255, 255) ' change to white (to ensure no cheating)
Print "White"
H = 120: V = 120 ' select point inside square
pixclr = Point(H, V) ' identify colour of point
Color pixclr ' change to this colour
PSet (200, 200)
Draw "r100d100l100u100" ' draw square in this colour
Quite simple, I guess. But how do I use this in a Color _RGB(r,g.b), _RGB(r,g,b) format? It doesn't allow _RGB(r,g,b),(pixclr)
You didn't look at what I posted in the demo prior to this. Your issue is, once again, *VARIABLE TYPE*.
You are not dimming pixclr as an _UNSIGNED LONG, which is needed when working with 32-bit colors. It's defaulting to SINGLE variable type, which lacks precision to hold the higher values of your colors.
Once you DIM pixclr AS _UNSIGNED LONG, you won't have as many issues making use of it.
Quote:But how do I use this in a Color _RGB(r,g.b), _RGB(r,g,b) format? It doesn't allow _RGB(r,g,b),(pixclr)
Didn't you just make use of it in both ways in your own code above??
Code: (Select All)
Color _RGB(255, 255, 255) ' change to white (to ensure no cheating)
Color pixclr ' change to this colour
That first statement uses _RGB(red, green, blue) values to set the color. The second statement uses a variable to set the color.
Isn't that what you just asked? How to use it in a color statement?
COLOR _RGB(255, 255, 255), pixclr '<--- This would set a foreground color of white (as you point out in your own code), and a background color of whatever the variable pixclr contains.
If you're asking about the PSET statement itself, the usage is just as simple as those two color commands which you've typed into your code above:
Code: (Select All)
PSET (x, y), _RGB(255, 255, 255) ' set the point (x, y) to become white.
PSET (x, y), pixclr ' set the point (x, y) to become whatever the variable pixclr contains.
You've successfully made use of _RGB in your code posted. You've successfully made use of a variable holding the color value. I really don't see what you're not getting here.
The only problem I see is you're not making your variable the proper type to work with 32-bit colors. SINGLE won't hold the values for 32-bit colors. You want to use _UNSIGNED LONG variable types instead.
Otherwise.... just use _RGB(red, green, blue) for your constant colors as you have been, and pixclr as your variable which holds those same values. You've used both properly in your own code above. What are you missing here? Where's the AHA lightbulb moment you're looking for?
Posts: 473
Threads: 70
Joined: Apr 2022
Reputation:
18
Hi Phil, I was looking at the wiki and Point(x,y) seems to be able to capture the color of a pixel at (x,y) so PixelColor = Point(x,y) from there I believe the directions would be to break out each individual red, green, blue ie R=_Red32(PixelColor) etc. Boy I could be so far out to lunch on this, I have often miss read the directions in the wiki and have never used Point before.
Posts: 3,447
Threads: 376
Joined: Apr 2022
Reputation:
345
(10-09-2025, 04:38 PM)Dimster Wrote: Hi Phil, I was looking at the wiki and Point(x,y) seems to be able to capture the color of a pixel at (x,y) so PixelColor = Point(x,y) from there I believe the directions would be to break out each individual red, green, blue ie R=_Red32(PixelColor) etc. Boy I could be so far out to lunch on this, I have often miss read the directions in the wiki and have never used Point before.
If you just want to copy the color and use it elsewhere, it's as simple as:
Code: (Select All)
DIM kolor AS _UNSIGNED LONG
kolor = POINT(x, y)
PSET (x1, y1), kolor
That's honestly all there is to it.
|