ColorHasAlpha()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

ColorHasAlpha()

Post 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.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: ColorHasAlpha()

Post 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)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: ColorHasAlpha()

Post 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).
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: ColorHasAlpha()

Post 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)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: ColorHasAlpha()

Post 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
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: ColorHasAlpha()

Post 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...
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
citystate
Enthusiast
Enthusiast
Posts: 638
Joined: Sun Feb 12, 2006 10:06 pm

Re: ColorHasAlpha()

Post 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
there is no sig, only zuul (and the following disclaimer)

WARNING: may be talking out of his hat
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: ColorHasAlpha()

Post 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
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Seymour Clufley
Addict
Addict
Posts: 1264
Joined: Wed Feb 28, 2007 9:13 am
Location: London

Re: ColorHasAlpha()

Post 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.
JACK WEBB: "Coding in C is like sculpting a statue using only sandpaper. You can do it, but the result wouldn't be any better. So why bother? Just use the right tools and get the job done."
User avatar
J. Baker
Addict
Addict
Posts: 2181
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: ColorHasAlpha()

Post 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.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Post Reply