Announcement

Collapse
No announcement yet.

Discuss TransparentText source code

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Discuss TransparentText source code

    https://forum.powerbasic.com/forum/u...und#post776899
    Example of using graphic controls to apply text with a transparent background
    to a graphic background while retaining dithering effects and porting them to new
    background.
    The module itself is less than thirty lines, if you don't count comments and allow for line stacking of commands.
    Code:
    SUB TransparentText(dlg AS DWORD, ctl AS LONG, strt AS STRING, xpos AS LONG, ypos AS LONG, hfont AS LONG, fgc AS LONG)
        LOCAL textcolor, tx, ty, bufsize, itr8, prcnt, minusp AS LONG
        LOCAL hbmp1 AS DWORD
        LOCAL textbuf, maskbuf AS STRING
        LOCAL lptr_textbuf, lptr_maskbuf AS LONG PTR
        LOCAL bgr_t, bgr_m, bgr_c AS bgra PTR
    ' get bgr value of text color and set bgr pointer to text color
        textcolor = BGR(fgc)
        bgr_c = VARPTR(textcolor)
    ' get size of working area
        GRAPHIC ATTACH dlg, ctl:  GRAPHIC SET FONT hfont:  tx = GRAPHIC(TEXT.SIZE.X, strt):  ty = GRAPHIC(TEXT.SIZE.Y, strt)
    ' create bitmap, print text to mask buffer and get background info
        GRAPHIC BITMAP NEW tx, ty TO hbmp1:  GRAPHIC ATTACH hbmp1, 0:  GRAPHIC SET FONT hfont
        GRAPHIC COLOR %WHITE, %BLACK:  GRAPHIC CLEAR:  GRAPHIC SET POS (0, 0):  GRAPHIC PRINT strt
        GRAPHIC GET BITS TO maskbuf:  GRAPHIC COPY dlg, ctl, (xpos, ypos)-(xpos + tx, ypos + ty) TO (0, 0)
        GRAPHIC GET BITS TO textbuf
    ' set up string buffers for writing text to background
        bufsize = (LEN(textbuf) \ 4) - 3
        lptr_textbuf = STRPTR(textbuf) + 8:  bgr_t = lptr_textbuf
        lptr_maskbuf = STRPTR(maskbuf) + 8:  bgr_m = lptr_maskbuf
    ' using value of text in mask, calculate and apply blending of text and background
        FOR itr8 = 0 TO bufsize
            @bgr_t[itr8].b = ((@bgr_c.b * @bgr_m[itr8].b) + (@bgr_t[itr8].b * (255 - @bgr_m[itr8].b))) \ 255
            @bgr_t[itr8].g = ((@bgr_c.g * @bgr_m[itr8].g) + (@bgr_t[itr8].g * (255 - @bgr_m[itr8].g))) \ 255
            @bgr_t[itr8].r = ((@bgr_c.r * @bgr_m[itr8].r) + (@bgr_t[itr8].r * (255 - @bgr_m[itr8].r))) \ 255
        NEXT
    ' copy edited rectangle back to original control end bitmap and font
        GRAPHIC ATTACH hbmp1, 0:  GRAPHIC SET BITS textbuf
        GRAPHIC ATTACH dlg, ctl, REDRAW:  GRAPHIC COPY hbmp1, 0, (0, 0)-(tx, ty) TO (xpos, ypos):  GRAPHIC REDRAW
        GRAPHIC ATTACH hbmp1, 0:  GRAPHIC BITMAP END:  FONT END hfont
    END SUB
    Example of calling code:
    Code:
        LOCAL hfont AS LONG
        CONTROL GET TEXT CB.HNDL, %dtext TO getvalue
        FONT NEW "Comic Sans MS", 16 TO hfont
        TransparentText CB.HNDL, %grfx, getvalue, 8, 20, hfont, %RGB_INDIGO
    The world is strange and wonderful.*
    I reserve the right to be horrifically wrong.
    Please maintain a safe following distance.
    *wonderful sold separately.

  • #2
    Hey Kurt,
    I'm not clear on something. Isn't this statement all that is required to draw transparent text?

    Code:
    Graphic Color %Black, -2
    Perhaps I'm not clear on what you're trying to demonstrate?

    Comment


    • #3
      Something like this ...

      Click image for larger version  Name:	pb_2080.jpg Views:	1 Size:	9.3 KB ID:	776905

      Code:
      'Compilable Example:
      #Compile Exe
      #Dim All
      %Unicode = 1
      #Include "Win32API.inc"
      %IDC_Graphic = 500
      
      Function PBMain() As Long
         Local hDlg, hFont As Dword, i As Long
         Font New "Tahoma",24,1 To hFont
         Dialog New Pixels, 0, "PowerBASIC",300,300,200,200, %WS_OverlappedWindow To hDlg
         Control Add Graphic, hDlg, %IDC_Graphic,"Push",0,0,200,200
         Graphic Attach hDlg, %IDC_Graphic
         Graphic Set Font hFont
         Graphic Width 2
         For i = 1 To 7 : Graphic Box (i*20,i*20)-(i*20+50,i*20+50) : Next i
         Graphic Color %Red, -2
         Graphic Set Pos(0,50) : Graphic Print "Howdy, Kurt!"
         Dialog Show Modal hDlg
      End Function

      Comment


      • #4
        Maybe this is what I've not understood?

        ... retaining dithering effects and porting them to new background.

        Comment


        • #5
          Originally posted by Gary Beene View Post
          Maybe this is what I've not understood?
          It does reconstruct what is already there, yes.
          But, you can also apply shadows that may be separately blurred.
          I was not even aware of the -2 color capability of graphic print!
          This would be useful for applying hand-drawn characters with dithering
          applied by a call to a blur routine prior to masked application.
          Probably not the most useful thing ever, but I wanted to master the
          graphics techniques used in gradient blending of foreground and background,
          especially as it applies to dithering, which is really just a limited type of a blur.
          I've reinvented the wheel, and put spinner hubs on it.
          The world is strange and wonderful.*
          I reserve the right to be horrifically wrong.
          Please maintain a safe following distance.
          *wonderful sold separately.

          Comment

          Working...
          X