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

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

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

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

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

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

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

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

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

    PRINT "=== BACKGROUND COLORS (bright) ==="
    FOR i = 100 TO 107
        IF i = 100 THEN name = "Black"
        IF i = 101 THEN name = "Red"
        IF i = 102 THEN name = "Green"
        IF i = 103 THEN name = "Yellow"
        IF i = 104 THEN name = "Blue"
        IF i = 105 THEN name = "Magenta"
        IF i = 106 THEN name = "Cyan"
        IF i = 107 THEN name = "White"
        CALL PrintColorSample(30, i, "  " & name & " (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, 4)
    NEXT bg
    PRINT
    FOR fg = 30 TO 37
        cell = " " & STR$(fg) & "  "
        PRINT MID$(cell, 1, 5)
        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, i, 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: PRINT CHR$(27) & \"[31m\" & \"text\" & CHR$(27) & \"[0m\""
    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
