Converting Transparency method from API1 to API2

I have an old Method to convert image to a certain transparency (create disabled button with canvas) and i tried to convert it to API2. My problem, is the original 100% transparency turn black for PNG images… any ideas ?

Heres the old code :

opacity = (1.0 - opacity)
dim x,y as integer
dim thePic as picture
dim c,white as color
white = Color.rgb(255, 255, 255)
if not base.HasAlphaChannel then base.transparent = 1
thePic = new Picture(base.width, base.height,32)
thePic.Graphics.drawpicture(base, 0, 0)

for x = 0 to thePic.width -  1
  for y = 0 to thePic.height -1
    c = thepic.graphics.pixel(x,y)
    if c <> Color.rgb(255,255,255)  then
      thepic.mask.graphics.pixel(x,y) = Color.rgb(opacity*255, opacity*255, opacity*255)
    else
      thepic.mask.graphics.pixel(x,y) = Color.rgb(255,255,255)
    end if
  next
next
if not thePic.HasAlphaChannel then thePic.transparent = 1

return thePic

and here what i think should work … but not…

opacity = 1.0 - opacity  // convert to alpha scale like old mask code

Var thePic As New Picture(base.Width, base.Height) // 32bpp w/ alpha
thePic.Graphics.DrawPicture(base, 0.0, 0.0)

Var surf As RGBSurface = thePic.RGBSurface
If surf <> Nil Then
  Var white As Color = Color.White
  Var a As Integer = CType(opacity * 255.0, Integer)  // truncates (0.73→186)
  If a < 0 Then a = 0
  If a > 255 Then a = 255
  
  For y As Integer = 0 To thePic.Height - 1
    For x As Integer = 0 To thePic.Width - 1
      Var c As Color = surf.Pixel(x, y)
      If c <> white Then
        // apply requested opacity to non-white pixels (0=opaque, 255=transparent)
        surf.Pixel(x, y) = Color.RGB(c.Red, c.Green, c.Blue, a)
      Else
        // make white fully transparent (like old mask white)
        surf.Pixel(x, y) = Color.RGB(c.Red, c.Green, c.Blue, 255)
      End If
    Next
  Next
End If

Return thePic

any suggestions ?

Nevermind ! instead of looking for white color or black color… i should look for alpha !

opacity = 1.0 - opacity  // convert to alpha scale like old mask code

Var thePic As New Picture(base.Width, base.Height) // 32bpp w/ alpha
thePic.Graphics.DrawPicture(base, 0.0, 0.0)

Var surf As RGBSurface = thePic.RGBSurface
If surf <> Nil Then
  Var white As Color = Color.White
  Var a As Integer = CType(opacity * 255.0, Integer)  // truncates (0.73→186)
  If a < 0 Then a = 0
  If a > 255 Then a = 255
  
  For y As Integer = 0 To thePic.Height - 1
    For x As Integer = 0 To thePic.Width - 1
      Var c As Color = surf.Pixel(x, y)
      If c.Alpha<250 Then
        // apply requested opacity to non-white pixels (0=opaque, 255=transparent)
        surf.Pixel(x, y) = Color.RGB(c.Red, c.Green, c.Blue, a)
      Else
        // make white fully transparent (like old mask white)
        surf.Pixel(x, y) = Color.RGB(c.Red, c.Green, c.Blue, 255)
      End If
    Next
  Next
End If

Return thePic

What’s wrong with using the standard .transparency instead?
Or do I miss something?

1 Like

From what I recall Transparency was only a binary value (ie only on or off), were as Alpha has 256 values allowing for softer fades and semi transparent colours.

graphics.transparency can hold values from 0 (not transparent) to 255 (fully transparent).

Sorry, I’m thinking of .Transparent :

According to the docs graphics.transparency runs from 0 to 100 and only affects drawing after you set it.

Deprecated if i understand… Xojo loves to remove useful functions and make it more painful

Good news and bad news. You are mistaken :slight_smile:

Were do you get that?

that’s not relative to a DesktopBevelButton

F**k me! My struggling with converting API1 to API2 pissed me off and made me blind! Thank you, Tim!