Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to text and background colors in screen 0?
#1
How do I get the color of text and the color of the background in screen 0 from a particular character? I figured out how to grab both  using mem but I'm not certain how to split those up from the value I grabbed.

Here's a sample program where a section of characters and the overall color attributes is read from a screen and saved into an array so the characters (with color attributes) can be reprinted elsewhere on the screen.  getctext let's me grab a run of characters and their overall color attributes. putctext let's me slap that run of characters back on the screen with the accurate color atributes but I'm just not sure how to break the color attributes apart.
Code: (Select All)
'$dynamic
ms = _NewImage(80, 28, 0)
Screen ms
Dim tx(0, 0)
Randomize Timer
For y = 1 To 12
    For x = 1 To 80
        Color Int(Rnd * 32), Int(Rnd * 16)
        _PrintString (x, y), Chr$(65 + Rnd * 26)
    Next x
Next y
getctext 1, 1, 960, ms, tx()
putctext 1, 15, tx(), ms




Sub getctext (x, y, cl, hndl, ar())
    'get text with color attributes from tetx screen at hndl upt ot cl charcaters starting at x,y and retunf that as a two dimensional array
    Dim o As _MEM
    ReDim ar(cl, 2)
    A$ = ""
    o = _MemImage(hndl)
    w = (_Width(hndl)) * 2
    ts = (y - 1) * w + (x - 1) * 2
    n = 0
    For px = 0 To (cl * 2 - 1) Step 2
        v = _MemGet(o, o.OFFSET + ts + px, _Unsigned _Byte) 'a PEEK
        c = _MemGet(o, o.OFFSET + ts + px + 1, _Unsigned _Byte)
        n = n + 1
        ar(n, 1) = v: ar(n, 2) = c
    Next px
    _MemFree o
End Sub
Sub putctext (x, y, ar(), hndl)
    'take a two dimensional array representing a colored text string ( ar() )  and place the charcters with proper color attributes on the screen
    Dim o As _MEM
    cl = UBound(ar)
    A$ = ""
    o = _MemImage(hndl)
    w = (_Width(hndl)) * 2
    ts = (y - 1) * w + (x - 1) * 2
    n = 0
    For cx = 1 To cl
        v = ar(cx, 1)
        c = ar(cx, 2)
        _MemPut o, o.OFFSET + ts + (cx - 1) * 2, v As _UNSIGNED _BYTE
        _MemPut o, o.OFFSET + ts + (cx - 1) * 2 + 1, c As _UNSIGNED _BYTE
    Next cx
    _MemFree o
End Sub


I'd also like to write a routine like this pritntctext(x,y,txt$,tcolor,bgcolor)  but I can't figure out how to correctly pull the text color and background colors out of that byte.   Anyone know how the color data is shoved in those 8 bits? I do know how it get and change bits , just not the order and how that one byte holds both the foreground and background.
Reply
#2
(09-22-2023, 07:51 PM)James D Jarvis Wrote: How do I get the color of text and the color of the background in screen 0 from a particular character? I figured out how to grab both  using mem but I'm not certain how to split those up from the value I grabbed.

Here's a sample program where a section of characters and the overall color attributes is read from a screen and saved into an array so the characters (with color attributes) can be reprinted elsewhere on the screen.  getctext let's me grab a run of characters and their overall color attributes. putctext let's me slap that run of characters back on the screen with the accurate color atributes but I'm just not sure how to break the color attributes apart.
Code: (Select All)
'$dynamic
ms = _NewImage(80, 28, 0)
Screen ms
Dim tx(0, 0)
Randomize Timer
For y = 1 To 12
    For x = 1 To 80
        Color Int(Rnd * 32), Int(Rnd * 16)
        _PrintString (x, y), Chr$(65 + Rnd * 26)
    Next x
Next y
getctext 1, 1, 960, ms, tx()
putctext 1, 15, tx(), ms




Sub getctext (x, y, cl, hndl, ar())
    'get text with color attributes from tetx screen at hndl upt ot cl charcaters starting at x,y and retunf that as a two dimensional array
    Dim o As _MEM
    ReDim ar(cl, 2)
    A$ = ""
    o = _MemImage(hndl)
    w = (_Width(hndl)) * 2
    ts = (y - 1) * w + (x - 1) * 2
    n = 0
    For px = 0 To (cl * 2 - 1) Step 2
        v = _MemGet(o, o.OFFSET + ts + px, _Unsigned _Byte) 'a PEEK
        c = _MemGet(o, o.OFFSET + ts + px + 1, _Unsigned _Byte)
        n = n + 1
        ar(n, 1) = v: ar(n, 2) = c
    Next px
    _MemFree o
End Sub
Sub putctext (x, y, ar(), hndl)
    'take a two dimensional array representing a colored text string ( ar() )  and place the charcters with proper color attributes on the screen
    Dim o As _MEM
    cl = UBound(ar)
    A$ = ""
    o = _MemImage(hndl)
    w = (_Width(hndl)) * 2
    ts = (y - 1) * w + (x - 1) * 2
    n = 0
    For cx = 1 To cl
        v = ar(cx, 1)
        c = ar(cx, 2)
        _MemPut o, o.OFFSET + ts + (cx - 1) * 2, v As _UNSIGNED _BYTE
        _MemPut o, o.OFFSET + ts + (cx - 1) * 2 + 1, c As _UNSIGNED _BYTE
    Next cx
    _MemFree o
End Sub


I'd also like to write a routine like this pritntctext(x,y,txt$,tcolor,bgcolor)  but I can't figure out how to correctly pull the text color and background colors out of that byte.   Anyone know how the color data is shoved in those 8 bits? I do know how it get and change bits , just not the order and how that one byte holds both the foreground and background.

Very complicated to use _MEM for this, you can get both, ASCII and color of a certain column/row position using the SCREEN function.

The color value is a combined byte value:
Bit 7 = blinking FG flag
Bit 6,5,4 = BG color (0-7)
Bit 3,2,1,0 = FG color (0-15)

See Wiki examples how to easily decode that.
Reply
#3
Thanks!

This is exactly what I needed.

The color value is a combined byte value:
Bit 7 = blinking FG flag
Bit 6,5,4 = BG color (0-7)
Bit 3,2,1,0 = FG color (0-15)
Reply
#4
see it in use in cprint and cfprint.
cprint and cfprint text mode routines (qb64phoenix.com)
Reply




Users browsing this thread: 1 Guest(s)