Page 1 of 1

ColorHasAlpha()

Posted: Wed Aug 03, 2011 1:13 am
by Seymour Clufley
I've noticed that RGB(255,0,0) and RGBA(255,0,0,255) give different integers, although they look identical visually.

Could we get a function that would tell us whether a colour integer has an alpha channel or not? It would be very helpful.

Re: ColorHasAlpha()

Posted: Wed Aug 03, 2011 1:56 am
by STARGÅTE

Code: Select all

Alpha(Color)
RGB(255,0,0) has no alpha-channel, so it returns 0
but you can not distinguish between RGB(255,0,0) and RGBA(255,0,0,0)

Re: ColorHasAlpha()

Posted: Wed Aug 03, 2011 1:55 pm
by Seymour Clufley
STARGÅTE wrote:RGB(255,0,0) has no alpha-channel, so it returns 0
That's the problem. Because it has no alpha channel, it will look like RGBA(255,0,0,255).

Re: ColorHasAlpha()

Posted: Wed Aug 03, 2011 2:24 pm
by STARGÅTE
From the color alone, you can't read the alpha value, because it is always 32 bits (long).

The only way is DrawingBufferPixelFormat, it returns:

Code: Select all

  #PB_PixelFormat_24Bits_RGB  ; 3 Byte pro Pixel (RRGGBB)
  #PB_PixelFormat_24Bits_BGR  ; 3 Byte pro Pixel (BBGGRR)
  #PB_PixelFormat_32Bits_RGB  ; 4 Byte pro Pixel (RRGGBB)
  #PB_PixelFormat_32Bits_BGR  ; 4 Byte pro Pixel (BBGGRR)

Re: ColorHasAlpha()

Posted: Wed Aug 03, 2011 3:24 pm
by Seymour Clufley
What about this?

Code: Select all

Procedure.b ColorHasAlpha(c.i)
    r = Red(c)
    g = Green(c)
    b = Blue(c)
    c2 = RGB(r,g,b)
    If c2=c
        ProcedureReturn #False
    Else
        ProcedureReturn #True
    EndIf
EndProcedure

Re: ColorHasAlpha()

Posted: Wed Aug 03, 2011 7:45 pm
by c4s
Sorry but your code doesn't make any sense. You can't detect if there is alpha. Either it is supported by the drawing output you are currently using and therefore present or not - It's as easy as that.

Maybe you should post what you need this function for. It maybe easier to help you then...

Re: ColorHasAlpha()

Posted: Wed Aug 03, 2011 10:02 pm
by citystate
I don't agree that the code doesn't make sense... however, RGB(255,0,0) would still return the same value as RGBA(255,0,0,0)

DrawingBufferPixelFormat is the easiest (perhaps the only) way

Re: ColorHasAlpha()

Posted: Wed Aug 03, 2011 11:13 pm
by J. Baker
Untested but wouldn't it be more like this?

Code: Select all

Procedure ColorHasAlpha(x, y)
       c = Point(x, y)
        a = Alpha(c)
       If a < 255
        ProcedureReturn #True
       Else
        ProcedureReturn #False
       EndIf
EndProcedure

Re: ColorHasAlpha()

Posted: Thu Aug 04, 2011 12:24 am
by Seymour Clufley
The point is that RGB(255,0,0) looks exactly the same as RGBA(255,0,0,255), but they have different numbers. RGBA(255,0,0,0) has the same number has RGB(255,0,0), but is completely invisible.

After further testing I can see that it's impossible to tell from the integer whether it has an alpha channel. A number produced with RGBA() is exactly the same as with RGB() if the alpha channel is 0. Only if you raise the alpha (to 1, for example) does RGBA() record it as a 32-bit colour. So I'm sorry for making this feature request as I can see it's impossible.

Why do I need to know the difference?

I'm building a system that takes values which could have been made using either RGB() or RGBA(). It stores them as numbers, and uses them later on. If the colour has alpha 0, does that mean the colour was made with RGB(), or with RGBA() using zero as the alpha value? Both have alpha 0, but one should be fully transparent and the other should be fully opaque.

That's the problem.

Re: ColorHasAlpha()

Posted: Thu Aug 04, 2011 12:38 am
by J. Baker
Forgive me if I'm missing something but you can't draw an alpha image with RGB alone as it always draws 255 for the alpha, so to speak. You could draw with RGB on a alpha layer but it still only draws the R, G, and B color information. So I would say if the color has an alpha value other then 255, save the info as RGBA, otherwise RGB.