Converts QBasic (MS-DOS) COLOR values into attribute number


Module Module5

    ' Converts MS-DOS foreground and background color values
    ' to an attribute byte used for display purposes
    Function AttributeValue(ByVal Fore As Integer, ByVal Back As Integer) As Integer
        ' Ensure Fore is in range 0-31 and Back in 0-15
        If Fore < 0 OrElse Fore > 31 Then
            Throw New ArgumentOutOfRangeException(NameOf(Fore), "Foreground must be between 0 and 31.")
        End If
        If Back < 0 OrElse Back > 15 Then
            Throw New ArgumentOutOfRangeException(NameOf(Back), "Background must be between 0 and 15.")
        End If

        ' Attribute byte: high nibble is background, low nibble is foreground
        Return ((Back And 15) << 4) Or (Fore And 31)
    End Function

    Sub Main5()
        ' Example usage:
        Console.WriteLine("MS-DOS Attribute Calculator")

        Console.Write("Enter foreground color (0-31): ")
        Dim fore As Integer = Integer.Parse(Console.ReadLine())

        Console.Write("Enter background color (0-15): ")
        Dim back As Integer = Integer.Parse(Console.ReadLine())

        Dim attr As Integer = AttributeValue(fore, back)

        Console.WriteLine($"The attribute value is: {attr}")
        Console.ReadLine()
    End Sub

End Module

Download 'QBasic COLOR values into attribute number':

📥 Download QBasic COLOR values into attribute number.vb