Author Topic: Anyone know how to make letters with a transparent background?  (Read 4406 times)

0 Members and 1 Guest are viewing this topic.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Anyone know how to make letters with a transparent background?
« Reply #1 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.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Anyone know how to make letters with a transparent background?
« Reply #2 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.  

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Anyone know how to make letters with a transparent background?
« Reply #3 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.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Anyone know how to make letters with a transparent background?
« Reply #4 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...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Anyone know how to make letters with a transparent background?
« Reply #5 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline johnno56

  • Forum Resident
  • Posts: 1270
  • Live long and prosper.
    • View Profile
Re: Anyone know how to make letters with a transparent background?
« Reply #6 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
Logic is the beginning of wisdom.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Anyone know how to make letters with a transparent background?
« Reply #7 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Anyone know how to make letters with a transparent background?
« Reply #8 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
« Last Edit: May 24, 2019, 12:15:31 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Anyone know how to make letters with a transparent background?
« Reply #9 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Anyone know how to make letters with a transparent background?
« Reply #10 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.  
« Last Edit: May 24, 2019, 01:27:07 pm by bplus »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Anyone know how to make letters with a transparent background?
« Reply #11 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
« Last Edit: May 24, 2019, 02:07:13 pm by Pete »
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Anyone know how to make letters with a transparent background?
« Reply #12 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.
« Last Edit: May 24, 2019, 02:45:54 pm by Petr »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Anyone know how to make letters with a transparent background?
« Reply #13 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.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Jack002

  • Forum Regular
  • Posts: 123
  • Boss, l wanna talk about arrays
    • View Profile
Re: Anyone know how to make letters with a transparent background?
« Reply #14 on: May 24, 2019, 03:51:45 pm »
COLOR 0,0 can really cut down on typos.

My tip of the day
QB64 is the best!