' ANSI Color Demo — foreground/background combinations via VT100 escape codes
#COMPILE EXE
#DIM ALL

SUB SetColor(BYVAL fg AS LONG, BYVAL bg AS LONG)
    PRINT CHR$(27) & "[" & STR$(fg) & ";" & STR$(bg + 10) & "m"
END SUB

SUB ResetColor()
    PRINT CHR$(27) & "[0m"
END SUB

SUB PrintColorSample(BYVAL fg AS LONG, BYVAL bg AS LONG, BYVAL sLabel AS STRING)
    CALL SetColor(fg, bg)
    PRINT sLabel
    CALL ResetColor()
    PRINT "  "
END SUB

SUB ShowForegroundColors()
    LOCAL i AS LONG
    LOCAL sName AS STRING

    PRINT "=== FOREGROUND COLORS (normal) ==="
    FOR i = 30 TO 37
        IF i = 30 THEN sName = "Black"
        IF i = 31 THEN sName = "Red"
        IF i = 32 THEN sName = "Green"
        IF i = 33 THEN sName = "Yellow"
        IF i = 34 THEN sName = "Blue"
        IF i = 35 THEN sName = "Magenta"
        IF i = 36 THEN sName = "Cyan"
        IF i = 37 THEN sName = "White"
        CALL PrintColorSample(i, 40, sName)
    NEXT i
    PRINT

    PRINT "=== FOREGROUND COLORS (bright) ==="
    FOR i = 90 TO 97
        IF i = 90 THEN sName = "Black"
        IF i = 91 THEN sName = "Red"
        IF i = 92 THEN sName = "Green"
        IF i = 93 THEN sName = "Yellow"
        IF i = 94 THEN sName = "Blue"
        IF i = 95 THEN sName = "Magenta"
        IF i = 96 THEN sName = "Cyan"
        IF i = 97 THEN sName = "White"
        CALL PrintColorSample(i, 40, sName)
    NEXT i
    PRINT
END SUB

SUB ShowBackgroundColors()
    LOCAL i AS LONG
    LOCAL sName AS STRING

    PRINT "=== BACKGROUND COLORS (normal) ==="
    FOR i = 40 TO 47
        IF i = 40 THEN sName = "Black"
        IF i = 41 THEN sName = "Red"
        IF i = 42 THEN sName = "Green"
        IF i = 43 THEN sName = "Yellow"
        IF i = 44 THEN sName = "Blue"
        IF i = 45 THEN sName = "Magenta"
        IF i = 46 THEN sName = "Cyan"
        IF i = 47 THEN sName = "White"
        CALL PrintColorSample(37, i, "  " & sName)
    NEXT i
    PRINT

    PRINT "=== BACKGROUND COLORS (bright) ==="
    FOR i = 100 TO 107
        IF i = 100 THEN sName = "Black"
        IF i = 101 THEN sName = "Red"
        IF i = 102 THEN sName = "Green"
        IF i = 103 THEN sName = "Yellow"
        IF i = 104 THEN sName = "Blue"
        IF i = 105 THEN sName = "Magenta"
        IF i = 106 THEN sName = "Cyan"
        IF i = 107 THEN sName = "White"
        CALL PrintColorSample(30, i, "  " & sName & " (bright)")
    NEXT i
    PRINT
END SUB

SUB ShowCombinations()
    LOCAL fg AS LONG
    LOCAL bg AS LONG
    LOCAL cell AS STRING

    PRINT "=== FOREGROUND/BACKGROUND MATRIX ==="
    PRINT "     "
    FOR bg = 40 TO 47
        cell = STR$(bg) & "  "
        PRINT MID$(cell,1)
    NEXT bg
    PRINT
    FOR fg = 30 TO 37
        cell = " " & STR$(fg) & "  "
        PRINT MID$(cell,1)
        FOR bg = 40 TO 47
            CALL SetColor(fg, bg)
            PRINT "  A  "
            CALL ResetColor()
        NEXT bg
        PRINT
    NEXT fg
    PRINT
END SUB

SUB ShowTextStyles()
    PRINT "=== TEXT STYLES ==="
    PRINT CHR$(27) & "[1m" & "Bold text" & CHR$(27) & "[0m"
    PRINT CHR$(27) & "[3m" & "Italic text" & CHR$(27) & "[0m"
    PRINT CHR$(27) & "[4m" & "Underlined text" & CHR$(27) & "[0m"
    PRINT CHR$(27) & "[5m" & "Blinking text" & CHR$(27) & "[0m"
    PRINT CHR$(27) & "[7m" & "Inverted text" & CHR$(27) & "[0m"
    PRINT CHR$(27) & "[9m" & "Strikethrough text" & CHR$(27) & "[0m"
    PRINT

    PRINT "=== COMBINED STYLES ==="
    PRINT CHR$(27) & "[1;4;31m" & "Bold, underlined, red" & CHR$(27) & "[0m"
    PRINT CHR$(27) & "[1;33;44m" & "Bold yellow on blue" & CHR$(27) & "[0m"
    PRINT CHR$(27) & "[3;36;41m" & "Italic cyan on red" & CHR$(27) & "[0m"
    PRINT
END SUB

SUB ShowColorBars()
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL bar AS STRING

    PRINT "=== COLOR BARS ==="
    FOR i = 41 TO 47
        CALL SetColor(30, i)
        bar = ""
        FOR j = 1 TO 30
            bar = bar & " "
        NEXT j
        PRINT bar
        CALL ResetColor()
        PRINT "  (bg=" & STR$(i) & ")"
    NEXT i
    PRINT

    PRINT "=== BRIGHT COLOR BARS ==="
    FOR i = 101 TO 107
        CALL SetColor(30, i)
        bar = ""
        FOR j = 1 TO 30
            bar = bar & " "
        NEXT j
        PRINT bar
        CALL ResetColor()
        PRINT "  (bright bg=" & STR$(i) & ")"
    NEXT i
    PRINT
END SUB

SUB ShowRainbow()
    LOCAL i AS LONG
    LOCAL text AS STRING
    LOCAL ch AS STRING
    LOCAL colorIdx AS LONG
    text = "RAINBOW COLORS DEMONSTRATION"
    PRINT "=== RAINBOW TEXT ==="
    FOR i = 1 TO LEN(text)
        ch = MID$(text,1)
        colorIdx = ((i - 1) MOD 7)
        IF colorIdx = 0 THEN CALL SetColor(31, 40)
        IF colorIdx = 1 THEN CALL SetColor(33, 40)
        IF colorIdx = 2 THEN CALL SetColor(32, 40)
        IF colorIdx = 3 THEN CALL SetColor(36, 40)
        IF colorIdx = 4 THEN CALL SetColor(34, 40)
        IF colorIdx = 5 THEN CALL SetColor(35, 40)
        IF colorIdx = 6 THEN CALL SetColor(37, 40)
        PRINT ch
        CALL ResetColor()
    NEXT i
    PRINT
    PRINT
END SUB

SUB ShowInfo()
    PRINT "=== ANSI ESCAPE REFERENCE ==="
    PRINT "CSI n m  where n = style code"
    PRINT "  0  Reset all"
    PRINT "  1  Bold / Bright"
    PRINT "  3  Italic"
    PRINT "  4  Underline"
    PRINT "  5  Blink"
    PRINT "  7  Inverse"
    PRINT "  9  Strikethrough"
    PRINT "  30-37  Foreground colors"
    PRINT "  40-47  Background colors"
    PRINT "  90-97  Bright foreground"
    PRINT " 100-107 Bright background"
    PRINT
    PRINT "Usage: GOTO ShowInfo for reference"
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "=============================="
    PRINT " ANSI Color Demo "
    PRINT "=============================="
    PRINT
    CALL ShowInfo()
    CALL ShowForegroundColors()
    CALL ShowBackgroundColors()
    CALL ShowCombinations()
    CALL ShowTextStyles()
    CALL ShowColorBars()
    CALL ShowRainbow()
    CALL ResetColor()
    PRINT "Demo complete."
    FUNCTION = 0
END FUNCTION
