QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Pete on May 23, 2019, 10:18:02 pm

Title: Anyone know how to make letters with a transparent background?
Post by: Pete on May 23, 2019, 10:18:02 pm
This should not be possible in SCREEN 0, so I was hoping for a graphics solution...

Can the foreground of a character be set to white, and its background be set to transparent? If so, a window with a black background on the top half, and a red background on the bottom half, might be able to have the white letters with a transparent background appear as white on black in the top half of the window, and white on red in the bottom half. I know, we are dealing with two actual layers here, one for the window background itself, and one for the characters, foreground and a transparent background, so I have no idea if this is possible in QB64, but hey, I'm no graphics guru, that's for sure but just to be clear, I know I could set the background of the letters to red or black, but that's not what I'm getting at. I'm thinking more of having a pattern of colors for the window, so it would be fast and easy if the background of the characters were transparent but slow and clumsy if I have to screen read the window color and adjust each character, printing them one at a time.

Thanks,

Pete
Title: Re: Anyone know how to make letters with a transparent background?
Post by: bplus on May 23, 2019, 11:46:12 pm
Easy set background of  COLOR to transparent color:
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(512, 512, 32)
  2. _TITLE "XOR Magic"
  3. COLOR &HFFFFFFFF, &H0 ' white foreground, transparent background
  4. i = 16
  5.     FOR y = 0 TO 512
  6.         FOR x = 0 TO 512
  7.             PSET (x, y), _RGB(x MOD 256, (x MOD 512 XOR y) MOD 256, y MOD 256)
  8.         NEXT
  9.     NEXT
  10.     _PRINTSTRING (150, i), "Hi Pete!"
  11.     _DISPLAY
  12.     _LIMIT 60
  13.     i = i + 1
  14.     IF i > 496 THEN i = 16
  15.  
  16.  
Title: Re: Anyone know how to make letters with a transparent background?
Post by: bplus on May 23, 2019, 11:56:19 pm
Print in transparent!
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(512, 512, 32)
  2. _TITLE "XOR Magic"
  3. COLOR &H0, &HFFFF0000 ' transparent foreground
  4. i = 16
  5.     FOR y = 0 TO 512
  6.         FOR x = 0 TO 512
  7.             PSET (x, y), _RGB(x MOD 256, (x MOD 512 XOR y) MOD 256, y MOD 256)
  8.         NEXT
  9.     NEXT
  10.     _PRINTSTRING (150, i), "  Hi Pete!  "
  11.     _DISPLAY
  12.     _LIMIT 60
  13.     i = i + 1
  14.     IF i > 496 THEN i = 16
  15.  
  16.  
Title: Re: Anyone know how to make letters with a transparent background?
Post by: Petr on May 24, 2019, 01:14:53 am
Code: QB64: [Select]
  1.  
  2. SCREEN _NEWIMAGE(640, 480, 32)
  3.  
  4. stp = 255 / 639
  5. FOR l = 0 TO 639
  6.     LINE (l, 0)-(l, 479), _RGB32(clr, clr, 255 - stp)
  7.     clr = clr + stp
  8.  
  9. COLOR _RGB32(0, 0, 0)
  10. _PRINTSTRING (180, 240), "Is _PRINTMODE, what Pete search?"
  11.  
  12.  
Title: Re: Anyone know how to make letters with a transparent background?
Post by: SMcNeill on May 24, 2019, 01:33:35 am
And you can also play around with custom routines like a few of my old print utilities: http://qb64.freeforums.net/thread/37/custom-routines-supports-textures-shading

Not only can you print transparent backgrounds, you can do oh so much more...
Title: Re: Anyone know how to make letters with a transparent background?
Post by: Pete on May 24, 2019, 02:17:06 am
Wow lots of responses. I'm only working on the first one so far, but I'll show you what I have so far...

Code: QB64: [Select]
  1. CONST HWND_TOPMOST%& = -1
  2. CONST SWP_NOSIZE%& = &H1
  3. CONST SWP_NOMOVE%& = &H2
  4. CONST SWP_SHOWWINDOW%& = &H40
  5.  
  6.     FUNCTION GetWindowLongA& (BYVAL hwnd AS LONG, BYVAL nIndex AS LONG)
  7.     FUNCTION SetWindowPos& (BYVAL hWnd AS LONG, BYVAL hWndInsertAfter AS _OFFSET, BYVAL X AS INTEGER, BYVAL Y AS INTEGER, BYVAL cx AS INTEGER, BYVAL cy AS INTEGER, BYVAL uFlags AS _OFFSET)
  8.     FUNCTION SetWindowLongA& (BYVAL hwnd AS LONG, BYVAL nIndex AS LONG, BYVAL dwNewLong AS LONG)
  9.     FUNCTION GetForegroundWindow&
  10.     FUNCTION SetLayeredWindowAttributes& (BYVAL hwnd AS LONG, BYVAL crKey AS LONG, BYVAL bAlpha AS _UNSIGNED _BYTE, BYVAL dwFlags AS LONG)
  11. SCREEN _NEWIMAGE(600, 480, 32)
  12. GWL_STYLE = -16
  13. ws_border = &H800000
  14. WS_VISIBLE = &H10000000
  15. _TITLE "Borderless Window"
  16. DIM hwnd AS LONG
  17. Level = 175
  18. SetWindowOpacity hwnd, Level
  19.  
  20. winstyle2& = GetWindowLongA&(hwnd, GWL_STYLE)
  21. winstyle& = -12582913
  22. a& = SetWindowLongA&(hwnd, GWL_STYLE, winstyle& AND WS_VISIBLE) ' AND NOT WS_VSCROLL) ' AND NOT ws_border)
  23. a& = SetWindowPos&(hwnd, 0, 0, 0, 0, 0, 39)
  24. msg$ = "Welcome to Translucent Windows Without Borders!"
  25. i = 16
  26. COLOR &HFFFFFFFF, &H0 ' white foreground, transparent background
  27.     _LIMIT 60
  28.     FGwin& = GetForegroundWindow&
  29.     IF hwnd <> FGwin& THEN ' QB64 no longer in focus.
  30.         WHILE _MOUSEINPUT: WEND
  31.         a& = SetWindowPos&(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE + SWP_SHOWWINDOW)
  32.         DO: _LIMIT 30: LOOP UNTIL hwnd = GetForegroundWindow&
  33.     END IF
  34.     IF INKEY$ = CHR$(27) THEN EXIT DO
  35.     FOR y = 480 TO 0 STEP -1
  36.         FOR x = 640 TO 0 STEP -1
  37.             PSET (x, 480 - y), _RGB(y / 2 MOD 256, 100, 200) ' (x MOD 512 XOR y) MOD 256, y MOD 256)
  38.         NEXT
  39.     NEXT
  40.     _PRINTSTRING (92, 1), msg$
  41.     _PRINTSTRING (1, 464), "Press Esc to quit."
  42.     _PRINTSTRING (250, i), "Hi Chez-Pete!"
  43.     _DISPLAY
  44.     i = i + 1
  45.     IF i > 464 THEN i = 16
  46.  
  47. SUB SetWindowOpacity (hwnd AS LONG, Level)
  48. DIM Msg AS LONG
  49. CONST G = -20
  50. CONST LWA_ALPHA = &H2
  51. CONST WS_EX_LAYERED = &H80000
  52. Msg = GetWindowLongA&(hwnd, G)
  53. Msg = Msg OR WS_EX_LAYERED
  54. action = SetWindowLongA&(hwnd, G, Msg)
  55. action = SetLayeredWindowAttributes(hwnd, 0, Level, LWA_ALPHA)
  56.  

I'm going to try to get some sleep now. I've been up too many days with a sick cat. My Son is taking finals this week, so I'm going to tough it out until he has his college stuff wrapped up. Thanks to all three of you for the interest in this topic. I'll look into Mark and Steve's post tomorrow. Also, if I stick with the color gradient, I would imagine that could be done faster with a _PRINTSTRING statement, to print across the width of the screen, instead of pixel by pixel.

Pete
Title: Re: Anyone know how to make letters with a transparent background?
Post by: johnno56 on May 24, 2019, 10:14:01 am
Oh nuts.  I get a 'DYNAMIC LIBRARY not found on line 6"

Would I be correct in assuming that this might be a reaction to a 64 bit Linux OS?

I changed nothing. Just copy / paste... Maybe it's the way I hold the Mouse?  lol
Title: Re: Anyone know how to make letters with a transparent background?
Post by: Pete on May 24, 2019, 11:00:38 am
Right. The code uses Windows Libraries. Sorry, I should have put that in the post. I have no idea if there would be a way to do this in Linux.

Pete
Title: Re: Anyone know how to make letters with a transparent background?
Post by: Pete on May 24, 2019, 11:52:16 am
@Mark: Ah, thanks for showing reverse transparency. That will come in handy for highlighting!

@ Steve: I'm following the link you posted now...

@Petr: Yes! _PRINTMODE is more what I was hoping for, thank you! Also _KEEPBACKGROUND. I modified the code to how I would use it, below...

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2.  
  3. stp = 255 / 479
  4. clr = 255
  5. FOR l = 0 TO 479
  6.     LINE (639, l)-(0, l), _RGB32(clr, clr, 255)
  7.     clr = clr - stp
  8.  
  9. COLOR _RGB32(0, 0, 0)
  10. _PRINTSTRING (180, 240), "_PRINTMODE, is what Pete was searching for!"
  11.  
  12.  

Now I need to go learn colors. Eventually, I want to get a white to yellow to orange blend.

Pete
Title: Re: Anyone know how to make letters with a transparent background?
Post by: SMcNeill on May 24, 2019, 12:41:24 pm
You can also easily use 0 alpha colors...

SCREEN _NEWIMAGE(640,480,32)
CLS &HFFAA0000 ‘red background
COLOR ,0 ‘notice the comma
PRINT “Hello World, transparent background”


It’s that simple.
Title: Re: Anyone know how to make letters with a transparent background?
Post by: bplus on May 24, 2019, 01:22:22 pm
Quote
Eventually, I want to get a white to yellow to orange blend.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2.  
  3. FOR l = 0 TO 213
  4.     midInk 255, 255, 255, 255, 255, 0, l / 214
  5.     LINE (639, l)-(0, l)
  6.  
  7.     midInk 255, 255, 0, 255, 100, 0, l / 214
  8.     LINE (639, l + 214)-(0, l + 214)
  9.  
  10.     midInk 255, 100, 0, 60, 25, 0, l / 214
  11.     LINE (639, l + 428)-(0, l + 428)
  12.  
  13.  
  14. COLOR _RGB32(0, 0, 0)
  15. s$ = "_PRINTMODE, is what Pete was searching for!"
  16. _PRINTSTRING ((_WIDTH - LEN(s$) * 8) / 2, 232), s$
  17.  
  18.  
  19. SUB midInk (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  20.     COLOR _RGB32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  21.  
  22.  
Title: Re: Anyone know how to make letters with a transparent background?
Post by: Pete on May 24, 2019, 01:54:28 pm
You guys are too fast for me. Remember what Qwerkey said, I'm old and I'm grumpy!

OK, I'll post this first, and then I'll take a look at Steve's alpha example and BP's blend. I was able to figure out the colors for yellow and orange. It was a single operation to blend near white to yellow, so I went with that in the example below. I'm pretty happy with the results!

Code: QB64: [Select]
  1. wdt = 640: hgt = 480
  2.  
  3. SCREEN _NEWIMAGE(wdt, hgt, 32)
  4.  
  5. stp = 255 / (hgt - 1)
  6. clr = 255
  7. FOR l = 0 TO (hgt - 1)
  8.     LINE ((wdt - 1), l)-(0, l), _RGB32(clr, clr, 204 - xlr)
  9.     xlr = xlr + stp / 2
  10. COLOR _RGB32(0, 0, 0)
  11.  
  12. LOCATE 1, 1
  13. FOR i = 1 TO wdt / 8 * (hgt - 16) / 16
  14.     DO
  15.         x = INT(RND * 30) + 1
  16.         IF x > 26 THEN
  17.             x = -64: IF flag = 0 THEN flag = -1: EXIT DO
  18.         ELSE
  19.             flag = 0: EXIT DO
  20.         END IF
  21.     LOOP
  22.     a$ = CHR$(96 + x)
  23.     x$ = x$ + a$
  24.  
  25. REDIM q$(hgt \ 16)
  26. m = 1
  27.     q$ = MID$(x$, m, wdt \ 8)
  28.     i = _INSTRREV(q$, " ")
  29.     IF i = 0 THEN m = m + wdt \ 8 ELSE m = m + i
  30.     q$ = MID$(q$, 1, i)
  31.     j = 0: xq$ = q$
  32.     DO
  33.         j = j + 1
  34.         z$ = MID$(x$, m, j)
  35.         IF z$ <> " " THEN EXIT DO
  36.         xq$ = xq$ + z$
  37.         m = m + 1
  38.     LOOP
  39.     cnt = cnt + 1
  40.     q$(cnt) = xq$
  41.     PRINT MID$(q$(cnt), 1, wdt \ 8);
  42.     IF cnt = hgt \ 16 - 1 THEN EXIT DO
  43.  

@BPlus: OK, Mark, that's pretty impressive. A bit above my pay grade for now, but I'll get the hang of it in time. Thanks!!!

@Steve: I had to tweak your code a bit to get it to work to...

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2. COLOR , &HFFAA0000 'red background
  3. COLOR , 0 'notice the comma
  4. PRINT "Hello World, transparent background!"

I was expecting to see black text on red, but it turned out to be bright white. I don't see foreground defined anywhere in the code, so huh???

Pete
Title: Re: Anyone know how to make letters with a transparent background?
Post by: Petr on May 24, 2019, 02:31:49 pm
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2. COLOR , &HFFAA0000 'red background
  3. COLOR &HFF000000, 0 'notice the comma
  4. PRINT "Hello World, transparent background!"
  5.  

Black text and transparent background

If you write COLOR &HFF000000 is the same as COLOR _RGBA32(0,0,0,255), with "&H" is it in this order: ALPHA, RED, GREEN, BLUE.
Title: Re: Anyone know how to make letters with a transparent background?
Post by: SMcNeill on May 24, 2019, 02:44:02 pm

@Steve: I had to tweak your code a bit to get it to work to...

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2. COLOR , &HFFAA0000 'red background
  3. COLOR , 0 'notice the comma
  4. PRINT "Hello World, transparent background!"

I was expecting to see black text on red, but it turned out to be bright white. I don't see foreground defined anywhere in the code, so huh???

Pete

If you don’t change the foreground color any, it defaults to white.  Think of the normal screen — white text on black background.

All we used the COLOR statement for, was to change the background:

COLOR , 0

No foreground change was made, so it stayed white.  The background became color 0 — which is 0 red, 0 Green, 0 Blue, 0 alpha, in 32-bit color values.  0 alpha is full transparency, so it’s just default white with no background, which we printed.
Title: Re: Anyone know how to make letters with a transparent background?
Post by: Jack002 on May 24, 2019, 03:51:45 pm
COLOR 0,0 can really cut down on typos.

My tip of the day
Title: Re: Anyone know how to make letters with a transparent background?
Post by: Petr on May 24, 2019, 05:01:16 pm
Quote
COLOR 0,0 can really cut down on typos.
This is Braill mode  :-D
Title: Re: Anyone know how to make letters with a transparent background?
Post by: Pete on May 24, 2019, 05:14:59 pm
Code: QB64: [Select]
  1.    
Title: Re: Anyone know how to make letters with a transparent background?
Post by: Pete on May 24, 2019, 07:16:04 pm
Well, that's the last time I take a "Tip of the Day" from Jack. Anyway, Here's Marks white-yellow-orange blend in a scallable version, meaning you can change the screen width and height variables, wdt and hgt, as desired.

Code: QB64: [Select]
  1. wdt = 340
  2. hgt = 580
  3. SCREEN _NEWIMAGE(wdt, hgt, 32)
  4. _SCREENMOVE 500, 100
  5. FOR l = 0 TO hgt * .45
  6.     midInk 255, 255, 255, 255, 255, 0, l / (hgt * .45)
  7.     LINE (wdt - 1, l)-(0, l)
  8.  
  9.     midInk 255, 255, 0, 255, 100, 0, l / (hgt * .45)
  10.     LINE (wdt - 1, l + hgt * .45 + 0)-(0, l + hgt * .45 + 0)
  11.  
  12.     midInk 255, 100, 0, 60, 25, 0, l / (hgt * .45)
  13.     LINE (wdt - 1, l + (hgt * .45 + 0) * 2)-(0, l + (hgt * .45 + 0) * 2)
  14.  
  15.  
  16. CALL getparagraph(wdt, hgt, x$)
  17. COLOR _RGB32(0, 0, 0)
  18. ' Center with: _PRINTSTRING ((_WIDTH - LEN(s$) * 8) / 2, 232), s$
  19. LOCATE 1, 1
  20. REDIM q$(hgt \ 16)
  21. m = 1
  22.     q$ = MID$(x$, m, wdt \ 8)
  23.     i = _INSTRREV(q$, " ")
  24.     IF i = 0 THEN m = m + wdt \ 8 ELSE m = m + i
  25.     q$ = MID$(q$, 1, i)
  26.     j = 0: xq$ = q$
  27.     DO
  28.         j = j + 1
  29.         z$ = MID$(x$, m, j)
  30.         IF z$ <> " " THEN EXIT DO
  31.         xq$ = xq$ + z$
  32.         m = m + 1
  33.     LOOP
  34.     cnt = cnt + 1
  35.     q$(cnt) = xq$
  36.     PRINT MID$(q$(cnt), 1, wdt \ 8);
  37.     IF cnt = hgt \ 16 - 1 THEN EXIT DO
  38.  
  39. SUB midInk (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  40. COLOR _RGB32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  41.  
  42. SUB getparagraph (wdt, hgt, x$)
  43. FOR i = 1 TO wdt / 8 * (hgt - 16) / 16
  44.     DO
  45.         x = INT(RND * 30) + 1
  46.         IF x > 26 THEN
  47.             x = -64: IF flag = 0 THEN flag = -1: EXIT DO
  48.         ELSE
  49.             flag = 0: EXIT DO
  50.         END IF
  51.     LOOP
  52.     a$ = CHR$(96 + x)
  53.     x$ = x$ + a$
  54.  

Pete
Title: Re: Anyone know how to make letters with a transparent background?
Post by: bplus on May 24, 2019, 10:11:49 pm
Make things more interesting, change width and height with p (plus) m (minus) keypress, PLUS some Braille:
Code: QB64: [Select]
  1. _TITLE "Try keypress m or p..."
  2. wdt = 800
  3. hgt = 600
  4. SCREEN _NEWIMAGE(wdt, hgt, 32)
  5. _SCREENMOVE 500, 100
  6. p = 4
  7.     k$ = INKEY$
  8.     IF k$ = "p" THEN
  9.         IF p < 64 THEN p = p * 2 ELSE p = 2
  10.         wdt = wdt + 5: hgt = hgt + 5
  11.     ELSEIF k$ = "m" THEN
  12.         IF p > 2 THEN p = p / 2 ELSE p = 64
  13.         wdt = wdt - 5: hgt = hgt - 5
  14.     END IF
  15.     SCREEN _NEWIMAGE(wdt, hgt, 32)
  16.     COLOR 15, 0
  17.     CLS
  18.  
  19.     FOR l = 0 TO hgt * .3333
  20.         midInk 255, 255, 255, 255, 255, 0, l / (hgt * .3333)
  21.         LINE (wdt - 1, l)-(0, l), , BF
  22.  
  23.         midInk 255, 255, 0, 255, 100, 0, l / (hgt * .3333)
  24.         LINE (wdt - 1, l + hgt * .3333 + 0)-(0, l + hgt * .3333 + 0), , BF
  25.  
  26.         midInk 255, 100, 0, 60, 25, 0, l / (hgt * .3333)
  27.         LINE (wdt - 1, l + (hgt * .3333 + 0) * 2)-(0, l + (hgt * .3333 + 0) * 2), , BF
  28.     NEXT
  29.     COLOR &HFFFFFF00
  30.     FOR y = 0 TO hgt STEP p
  31.         FOR x = 0 TO wdt STEP p
  32.             IF y MOD 2 * p = 0 THEN
  33.                 IF x MOD 2 * p = 0 THEN
  34.                     CIRCLE (x, y), 1
  35.                     PSET (x, y)
  36.                 END IF
  37.             ELSEIF y MOD 2 * p = p THEN
  38.                 IF x MOD 2 * p = p THEN
  39.                     CIRCLE (x, y), 1
  40.                     PSET (x, y)
  41.                 END IF
  42.             END IF
  43.         NEXT
  44.     NEXT
  45.  
  46.  
  47.  
  48.     CALL getparagraph(wdt, hgt, x$)
  49.     COLOR _RGB32(0, 0, 0)
  50.     ' Center with: _PRINTSTRING ((_WIDTH - LEN(s$) * 8) / 2, 232), s$
  51.     LOCATE 1, 1
  52.     REDIM q$(hgt \ 16)
  53.     m = 1: cnt = 0
  54.     DO
  55.         q$ = MID$(x$, m, wdt \ 8)
  56.         i = _INSTRREV(q$, " ")
  57.         IF i = 0 THEN m = m + wdt \ 8 ELSE m = m + i
  58.         q$ = MID$(q$, 1, i)
  59.         j = 0: xq$ = q$
  60.         DO
  61.             j = j + 1
  62.             z$ = MID$(x$, m, j)
  63.             IF z$ <> " " THEN EXIT DO
  64.             xq$ = xq$ + z$
  65.             m = m + 1
  66.         LOOP
  67.         cnt = cnt + 1
  68.         q$(cnt) = xq$
  69.         PRINT MID$(q$(cnt), 1, wdt \ 8);
  70.         IF cnt = hgt \ 16 - 1 THEN EXIT DO
  71.     LOOP
  72.     _DISPLAY
  73.     _LIMIT 60
  74.  
  75.  
  76. SUB midInk (r1%, g1%, b1%, r2%, g2%, b2%, fr##)
  77.     COLOR _RGB32(r1% + (r2% - r1%) * fr##, g1% + (g2% - g1%) * fr##, b1% + (b2% - b1%) * fr##)
  78.  
  79. SUB getparagraph (wdt, hgt, x$)
  80.     FOR i = 1 TO wdt / 8 * (hgt - 16) / 16
  81.         DO
  82.             x = INT(RND * 30) + 1
  83.             IF x > 26 THEN
  84.                 x = -64: IF flag = 0 THEN flag = -1: EXIT DO
  85.             ELSE
  86.                 flag = 0: EXIT DO
  87.             END IF
  88.         LOOP
  89.         a$ = CHR$(96 + x)
  90.         x$ = x$ + a$
  91.     NEXT
  92.  
  93.  

EDIT: fix Pete's midInk Math