Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detecting color depth-16 color, 256 color, or 32 bit color
#1
I am changing all of my libraries to be agnostic to color depth...or at least trying.  I want them to automatically detect whether we are in 16 color, 256 color, or 32 bit color mode and automatically make adjustments on the fly to compensate for the colors being used, and use the proper colors.

So far so good, EXCEPT that I am having an issue with accurately detecting whether we are in 16 color, 256 color, or 32 bit color.

I cannot find this as a command for QB64.  Is there an easy way to accurately detect this?
Reply
#2
_PIXELSIZE should be help.
Reply
#3
There's an issue with trying to sort between 16-color and 256-color images.  The problem is that both use 1 byte-per-pixel in memory, so you may need to compare against _PALETTECOLOR to distinguish between the two.

Try this:
Code: (Select All)
Dim a(3)
a(1) = _NewImage(640, 480, 32)
a(2) = _NewImage(640, 480, 256)
a(3) = _NewImage(640, 480, 12)


For i = 1 To 3
Screen a(i)
Select Case _PixelSize
Case 4: Print "32-bit color"
Case 1:
cu = 0
For j = 0 To 255
If _PaletteColor(j) <> &HFF000000 Then cu = cu + 1
Next
Select Case cu
Case Is <= 16: Print "16 color image"
Case Else: Print "256 color image"
End Select
End Select
Sleep
Next
Reply
#4
(08-16-2024, 12:55 AM)SMcNeill Wrote: There's an issue with trying to sort between 16-color and 256-color images.  The problem is that both use 1 byte-per-pixel in memory, so you may need to compare against _PALETTECOLOR to distinguish between the two.
Works like a charm.  Once again, thank you Steve!
Reply
#5
(08-16-2024, 01:04 PM)dano Wrote:
(08-16-2024, 12:55 AM)SMcNeill Wrote: There's an issue with trying to sort between 16-color and 256-color images.  The problem is that both use 1 byte-per-pixel in memory, so you may need to compare against _PALETTECOLOR to distinguish between the two.
Works like a charm.  Once again, thank you Steve!

Note that it's not fool-proof.  Most things seldom are.  The power of fools is astounding!

Some fool out there might have a 256-color image, and 240 of the colors are all full alpha BLACK...  That'd still detect as a 16-color palette.

It's not *likely* to happen to you, but one should never rule out the impossible with stuff.  Just keep it in the back of your mind, in case you ever get some odd glitches sometime and need to deal with them.
Reply
#6
(08-16-2024, 12:55 AM)SMcNeill Wrote: There's an issue with trying to sort between 16-color and 256-color images.  The problem is that both use 1 byte-per-pixel in memory, so you may need to compare against _PALETTECOLOR to distinguish between the two.

Try this:
Code: (Select All)
Dim a(3)
a(1) = _NewImage(640, 480, 32)
a(2) = _NewImage(640, 480, 256)
a(3) = _NewImage(640, 480, 12)


For i = 1 To 3
    Screen a(i)
    Select Case _PixelSize
        Case 4: Print "32-bit color"
        Case 1:
            cu = 0
            For j = 0 To 255
                If _PaletteColor(j) <> &HFF000000 Then cu = cu + 1
            Next
            Select Case cu
                Case Is <= 16: Print "16 color image"
                Case Else: Print "256 color image"
            End Select
    End Select
    Sleep
Next
There's that thinking outside the box again. I've never bothered trying to identify 16 color versus 256 color since _PIXELSIZE can't make the distinction. _PALETTECOLOR was staring me in the face the whole time.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#7
I modified the code a bit so you don't need to use SCREEN to get the color depth of the images.

Code: (Select All)
DIM a(3)
a(1) = _NEWIMAGE(640, 480, 32)
a(2) = _NEWIMAGE(640, 480, 256)
a(3) = _NEWIMAGE(640, 480, 12)
FOR i = 1 TO 3
    SELECT CASE _PIXELSIZE(a(i))
        CASE 4: PRINT "32-bit color"
        CASE 1:
            cu = 0
            FOR j = 0 TO 255
                IF _PALETTECOLOR(j, a(i)) <> &HFF000000 THEN cu = cu + 1
            NEXT
            SELECT CASE cu
                CASE IS <= 16: PRINT "16 color image"
                CASE ELSE: PRINT "256 color image"
            END SELECT
    END SELECT
    SLEEP
NEXT
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#8
Thank you.  Does this also mean that there is an easy way to read the current foreground and background colors regardless of the SCREEN setting or 16/256/32bit color?
Reply
#9
_DEFAULTCOLOR
_BACKGROUNDCOLOR
Reply
#10
(08-16-2024, 05:52 PM)dano Wrote: Thank you.  Does this also mean that there is an easy way to read the current foreground and background colors regardless of the SCREEN setting or 16/256/32bit color?

_DefaultColor and _BackgroundColor
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question using $color:32 colour names mid-program? hskakw 2 195 01-14-2026, 09:30 AM
Last Post: hskakw
Question Latest version of QB64PE or QB64 compatible with Windows XP (32-bit)? madscijr 14 1,932 09-30-2025, 08:10 AM
Last Post: hsiangch_ong
  Test to post a new Thread + question 32 or 64 bit Rudy M 2 529 09-09-2025, 04:10 PM
Last Post: Rudy M
  useful page: Value of constants for the Windows 32-bit API madscijr 4 662 08-15-2025, 06:35 PM
Last Post: SMcNeill
  _CLEARCOLOR evety color EXCEPT ...? madscijr 4 824 04-27-2025, 10:51 PM
Last Post: TempodiBasic

Forum Jump:


Users browsing this thread: 1 Guest(s)