An easy way to use X11 Color Scheme in PB Programs with and without Alpha

Share your advanced PureBasic knowledge/code with the community.
SMaag
Enthusiast
Enthusiast
Posts: 325
Joined: Sat Jan 14, 2023 6:55 pm
Location: Bavaria/Germany

An easy way to use X11 Color Scheme in PB Programs with and without Alpha

Post by SMaag »

here is an early implemnetation of the X11 Color Scheme what keeps all Colors in Datasection and point on it with a virtual Array.
With the Macro X11Col() it is easy to use like a standard Array.
With the Procedure SetTableAlpha(AlphaValue) you can set the Alpha vlaue of all Colors to a specific vale. This is convinient if you will use it with VectorDrawing library what needs a Alpha otherwise it will draw nothing because the color is full transparent.

Update with AutoChangeByteOrder to System Order $RGB to $BGR

Code: Select all

; X11 Color-Table for easy use
; should work on all OS

EnableExplicit

Enumeration EX11ColID 0   ; Attention! has to be same order as in DataSection; Colors are orderd by RGB-Value; 
  ; ID [0..144]
  #X11Col_ID_Black
  #X11Col_ID_NavyBlue
  #X11Col_ID_DarkBlue
  #X11Col_ID_MediumBlue
  #X11Col_ID_Blue
  #X11Col_ID_DarkGreen
  #X11Col_ID_WebGreen
  #X11Col_ID_Teal
  #X11Col_ID_DarkCyan
  #X11Col_ID_DeepSkyBlue
  #X11Col_ID_DarkTurquoise
  #X11Col_ID_MediumSpringGreen
  #X11Col_ID_Green
  #X11Col_ID_Lime
  #X11Col_ID_SpringGreen
  #X11Col_ID_Aqua
  #X11Col_ID_Cyan
  #X11Col_ID_MidnightBlue
  #X11Col_ID_DodgerBlue
  #X11Col_ID_LightSeaGreen
  #X11Col_ID_ForestGreen
  #X11Col_ID_SeaGreen
  #X11Col_ID_DarkSlateGray
  #X11Col_ID_LimeGreen
  #X11Col_ID_MediumSeaGreen
  #X11Col_ID_Turquoise
  #X11Col_ID_RoyalBlue
  #X11Col_ID_SteelBlue
  #X11Col_ID_DarkSlateBlue
  #X11Col_ID_MediumTurquoise
  #X11Col_ID_Indigo
  #X11Col_ID_DarkOliveGreen
  #X11Col_ID_CadetBlue
  #X11Col_ID_CornflowerBlue
  #X11Col_ID_RebeccaPurple
  #X11Col_ID_MediumAquamarine
  #X11Col_ID_DimGray
  #X11Col_ID_SlateBlue
  #X11Col_ID_OliveDrab
  #X11Col_ID_SlateGray
  #X11Col_ID_LightSlateGray
  #X11Col_ID_MediumSlateBlue
  #X11Col_ID_LawnGreen
  #X11Col_ID_Chartreuse
  #X11Col_ID_Aquamarine
  #X11Col_ID_WebMaroon
  #X11Col_ID_WebPurple
  #X11Col_ID_Olive
  #X11Col_ID_WebGray
  #X11Col_ID_SkyBlue
  #X11Col_ID_LightSkyBlue
  #X11Col_ID_BlueViolet
  #X11Col_ID_DarkRed
  #X11Col_ID_DarkMagenta
  #X11Col_ID_SaddleBrown
  #X11Col_ID_DarkSeaGreen
  #X11Col_ID_LightGreen
  #X11Col_ID_MediumPurple
  #X11Col_ID_DarkViolet
  #X11Col_ID_PaleGreen
  #X11Col_ID_DarkOrchid
  #X11Col_ID_YellowGreen
  #X11Col_ID_Purple
  #X11Col_ID_Sienna
  #X11Col_ID_Brown
  #X11Col_ID_DarkGray
  #X11Col_ID_LightBlue
  #X11Col_ID_GreenYellow
  #X11Col_ID_PaleTurquoise
  #X11Col_ID_Maroon
  #X11Col_ID_LightSteelBlue
  #X11Col_ID_PowderBlue
  #X11Col_ID_Firebrick
  #X11Col_ID_DarkGoldenrod
  #X11Col_ID_MediumOrchid
  #X11Col_ID_RosyBrown
  #X11Col_ID_DarkKhaki
  #X11Col_ID_Gray
  #X11Col_ID_Silver
  #X11Col_ID_MediumVioletRed
  #X11Col_ID_IndianRed
  #X11Col_ID_Peru
  #X11Col_ID_Chocolate
  #X11Col_ID_Tan
  #X11Col_ID_LightGray
  #X11Col_ID_Thistle
  #X11Col_ID_Orchid
  #X11Col_ID_Goldenrod
  #X11Col_ID_PaleVioletRed
  #X11Col_ID_Crimson
  #X11Col_ID_Gainsboro
  #X11Col_ID_Plum
  #X11Col_ID_Burlywood
  #X11Col_ID_LightCyan
  #X11Col_ID_Lavender
  #X11Col_ID_DarkSalmon
  #X11Col_ID_Violet
  #X11Col_ID_PaleGoldenrod
  #X11Col_ID_LightCoral
  #X11Col_ID_Khaki
  #X11Col_ID_AliceBlue
  #X11Col_ID_Honeydew
  #X11Col_ID_Azure
  #X11Col_ID_SandyBrown
  #X11Col_ID_Wheat
  #X11Col_ID_Beige
  #X11Col_ID_WhiteSmoke
  #X11Col_ID_MintCream
  #X11Col_ID_GhostWhite
  #X11Col_ID_Salmon
  #X11Col_ID_AntiqueWhite
  #X11Col_ID_Linen
  #X11Col_ID_LightGoldenrod
  #X11Col_ID_OldLace
  #X11Col_ID_Red
  #X11Col_ID_Fuchsia
  #X11Col_ID_Magenta
  #X11Col_ID_DeepPink
  #X11Col_ID_OrangeRed
  #X11Col_ID_Tomato
  #X11Col_ID_HotPink
  #X11Col_ID_Coral
  #X11Col_ID_DarkOrange
  #X11Col_ID_LightSalmon
  #X11Col_ID_Orange
  #X11Col_ID_LightPink
  #X11Col_ID_Pink
  #X11Col_ID_Gold
  #X11Col_ID_PeachPuff
  #X11Col_ID_NavajoWhite
  #X11Col_ID_Moccasin
  #X11Col_ID_Bisque
  #X11Col_ID_MistyRose
  #X11Col_ID_BlanchedAlmond
  #X11Col_ID_PapayaWhip
  #X11Col_ID_LavenderBlush
  #X11Col_ID_Seashell
  #X11Col_ID_Cornsilk
  #X11Col_ID_LemonChiffon
  #X11Col_ID_FloralWhite
  #X11Col_ID_Snow
  #X11Col_ID_Yellow
  #X11Col_ID_LightYellow
  #X11Col_ID_Ivory
  #X11Col_ID_White
EndEnumeration

#X11Col_MaxID = #PB_Compiler_EnumerationValue - 1  ; = 145-1 = 144, the max X11Col-ID

Debug "X11Col_MaxID = " +  #X11Col_MaxID
; Global Macro to Get X11Color
     
Structure pX11ColTable
  col.l[0]      ; virtual Array  
EndStructure

Global *X11Col.pX11ColTable     ; Pointer to a virtual Color Array

*X11Col= ?X11Col                ; Set the Pointer to virtual Color Array = DataSection X11Col

; Macro to use X11Col() like a standard Array
Macro X11Col(X11ColorID)
  *X11Col\col[X11ColorID] 
EndMacro

; Macro for validity check of color ID [0..144]
Macro X11ColID_Check(X11ColorID)
  Bool(X11ColorID>=0 And X11ColorID<=#X11Col_MaxID)
EndMacro

; because of Unix notation of the Colors $RGB we have to change to Windows ByteOrder $BGR
Procedure ToggleTableByteOrder()
  Protected.i I
  Protected.q col
  
  For I = 0 To #X11Col_MaxID
    col= *X11Col\col[I] ; read color from Datasection X11Col-Table
    *X11Col\col[I] = RGBA( Blue(col), Green(col), Red(col), Alpha(Col)) ; create the Color with Changed ByteOrder, Swap Red,Blue
  Next
EndProcedure

; AutoChange the $RGB to $BGR if necessary
If #Red <> X11Col(#X11Col_ID_Red)
  ToggleTableByteOrder()
  Debug "X11Col-Table ByteOrder $RGB automatically changed to System ByteOrder $BGR" 
  ;MessageRequester("RGB ByteOrder Changed", "RGB-Byte-Order of X11 Color Table changed to system order")
EndIf
  
Procedure SetTableAlpha(NewAlpha=255)
  ; sets the Alpha-Channel of all Colors in the DataSection to NewAlpha
  Protected.i I
  Protected.q col
  
  For I = 0 To #X11Col_MaxID
    col= *X11Col\col[I] ; read color from Datasection X11Col-Table
    *X11Col\col[I] = RGBA(Red(col), Green(col), Blue(col), NewAlpha) ; create the Color with Alpha
  Next
EndProcedure

DisableExplicit

DataSection
  ; Attention Unix notation $RGB, for Windows we need $BGR
  X11Col:; $RGB
  Data.l $000000 ; Black
  Data.l $000080 ; Navy Blue
  Data.l $00008B ; Dark Blue
  Data.l $0000CD ; Medium Blue
  Data.l $0000FF ; Blue
  Data.l $006400 ; Dark Green
  Data.l $008000 ; Web Green
  Data.l $008080 ; Teal
  Data.l $008B8B ; Dark Cyan
  Data.l $00BFFF ; Deep Sky Blue
  Data.l $00CED1 ; Dark Turquoise
  Data.l $00FA9A ; Medium Spring Green
  Data.l $00FF00 ; Green
  Data.l $00FF00 ; Lime
  Data.l $00FF7F ; Spring Green
  Data.l $00FFFF ; Aqua
  Data.l $00FFFF ; Cyan
  Data.l $191970 ; Midnight Blue
  Data.l $1E90FF ; Dodger Blue
  Data.l $20B2AA ; Light Sea Green
  Data.l $228B22 ; Forest Green
  Data.l $2E8B57 ; Sea Green
  Data.l $2F4F4F ; Dark Slate Gray
  Data.l $32CD32 ; Lime Green
  Data.l $3CB371 ; Medium Sea Green
  Data.l $40E0D0 ; Turquoise
  Data.l $4169E1 ; Royal Blue
  Data.l $4682B4 ; Steel Blue
  Data.l $483D8B ; Dark Slate Blue
  Data.l $48D1CC ; Medium Turquoise
  Data.l $4B0082 ; Indigo
  Data.l $556B2F ; Dark Olive Green
  Data.l $5F9EA0 ; Cadet Blue
  Data.l $6495ED ; Cornflower Blue
  Data.l $663399 ; Rebecca Purple
  Data.l $66CDAA ; Medium Aquamarine
  Data.l $696969 ; Dim Gray
  Data.l $6A5ACD ; Slate Blue
  Data.l $6B8E23 ; Olive Drab
  Data.l $708090 ; Slate Gray
  Data.l $778899 ; Light Slate Gray
  Data.l $7B68EE ; Medium Slate Blue
  Data.l $7CFC00 ; Lawn Green
  Data.l $7FFF00 ; Chartreuse
  Data.l $7FFFD4 ; Aquamarine
  Data.l $800000 ; Web Maroon
  Data.l $800080 ; Web Purple
  Data.l $808000 ; Olive
  Data.l $808080 ; Web Gray
  Data.l $87CEEB ; Sky Blue
  Data.l $87CEFA ; Light Sky Blue
  Data.l $8A2BE2 ; Blue Violet
  Data.l $8B0000 ; Dark Red
  Data.l $8B008B ; Dark Magenta
  Data.l $8B4513 ; Saddle Brown
  Data.l $8FBC8F ; Dark Sea Green
  Data.l $90EE90 ; Light Green
  Data.l $9370DB ; Medium Purple
  Data.l $9400D3 ; Dark Violet
  Data.l $98FB98 ; Pale Green
  Data.l $9932CC ; Dark Orchid
  Data.l $9ACD32 ; Yellow Green
  Data.l $A020F0 ; Purple
  Data.l $A0522D ; Sienna
  Data.l $A52A2A ; Brown
  Data.l $A9A9A9 ; Dark Gray
  Data.l $ADD8E6 ; Light Blue
  Data.l $ADFF2F ; Green Yellow
  Data.l $AFEEEE ; Pale Turquoise
  Data.l $B03060 ; Maroon
  Data.l $B0C4DE ; Light Steel Blue
  Data.l $B0E0E6 ; Powder Blue
  Data.l $B22222 ; Firebrick
  Data.l $B8860B ; Dark Goldenrod
  Data.l $BA55D3 ; Medium Orchid
  Data.l $BC8F8F ; Rosy Brown
  Data.l $BDB76B ; Dark Khaki
  Data.l $BEBEBE ; Gray
  Data.l $C0C0C0 ; Silver
  Data.l $C71585 ; Medium Violet Red
  Data.l $CD5C5C ; Indian Red
  Data.l $CD853F ; Peru
  Data.l $D2691E ; Chocolate
  Data.l $D2B48C ; Tan
  Data.l $D3D3D3 ; Light Gray
  Data.l $D8BFD8 ; Thistle
  Data.l $DA70D6 ; Orchid
  Data.l $DAA520 ; Goldenrod
  Data.l $DB7093; Pale Violet Red
  Data.l $DC143C ; Crimson
  Data.l $DCDCDC ; Gainsboro
  Data.l $DDA0DD ; Plum
  Data.l $DEB887 ; Burlywood
  Data.l $E0FFFF ; Light Cyan
  Data.l $E6E6FA ; Lavender
  Data.l $E9967A ; Dark Salmon
  Data.l $EE82EE ; Violet
  Data.l $EEE8AA ; Pale Goldenrod
  Data.l $F08080 ; Light Coral
  Data.l $F0E68C ; Khaki
  Data.l $F0F8FF ; Alice Blue
  Data.l $F0FFF0 ; Honeydew
  Data.l $F0FFFF ; Azure
  Data.l $F4A460 ; Sandy Brown
  Data.l $F5DEB3 ; Wheat
  Data.l $F5F5DC ; Beige
  Data.l $F5F5F5 ; White Smoke
  Data.l $F5FFFA ; Mint Cream
  Data.l $F8F8FF ; Ghost White
  Data.l $FA8072 ; Salmon
  Data.l $FAEBD7 ; Antique White
  Data.l $FAF0E6 ; Linen
  Data.l $FAFAD2 ; Light Goldenrod
  Data.l $FDF5E6 ; Old Lace
  Data.l $FF0000 ; Red
  Data.l $FF00FF ; Fuchsia
  Data.l $FF00FF ; Magenta
  Data.l $FF1493 ; Deep Pink
  Data.l $FF4500 ; Orange Red
  Data.l $FF6347 ; Tomato
  Data.l $FF69B4 ; Hot Pink
  Data.l $FF7F50 ; Coral
  Data.l $FF8C00 ; Dark Orange
  Data.l $FFA07A ; Light Salmon
  Data.l $FFA500 ; Orange
  Data.l $FFB6C1 ; Light Pink
  Data.l $FFC0CB ; Pink
  Data.l $FFD700 ; Gold
  Data.l $FFDAB9 ; Peach Puff
  Data.l $FFDEAD ; Navajo White
  Data.l $FFE4B5 ; Moccasin
  Data.l $FFE4C4 ; Bisque
  Data.l $FFE4E1 ; Misty Rose
  Data.l $FFEBCD ; Blanched Almond
  Data.l $FFEFD5 ; Papaya Whip
  Data.l $FFF0F5 ; Lavender Blush
  Data.l $FFF5EE ; Seashell
  Data.l $FFF8DC ; Cornsilk
  Data.l $FFFACD ; Lemon Chiffon
  Data.l $FFFAF0 ; Floral White
  Data.l $FFFAFA ; Snow
  Data.l $FFFF00 ; Yellow
  Data.l $FFFFE0 ; Light Yellow
  Data.l $FFFFF0 ; Ivory
  Data.l $FFFFFF ; White
EndDataSection


CompilerIf  #PB_Compiler_IsMainFile
  
  
  Define I 
  
  Debug ""
  Debug "---------------------------------------------------" 
  Debug "ByteOrderCheck - the PB_Red/Green/Blue must be the same value," 
  Debug "  otherwise the Table To System Byteorder is wrong"
  Debug ""
  Debug "X11_Red = " + Hex( X11Col(#X11Col_ID_Red) ,#PB_Long)
  Debug "PB__Red = " + Hex(#Red ,#PB_Long)
  Debug ""
  Debug "X11_Green = " + Hex( X11Col(#X11Col_ID_Green) ,#PB_Long)  
  Debug "PB__Green = " + Hex( #Green ,#PB_Long)
  Debug ""
  Debug "X11_Blue  = " + Hex( X11Col(#X11Col_ID_Blue) ,#PB_Long)
  Debug "PB__Blue  = " + Hex(#Blue ,#PB_Long)
  Debug "---------------------------------------------------"

  
  ; For Each X11Colors print HexValue - without Alpha
  For I = 0 To #X11Col_MaxID
    Debug Str(I) + " : " + Hex( *X11Col\col[I],#PB_Long )
  Next
  
  Debug ""
  Debug "---------------------------------------------------"
  Debug " with Alpha"
  Debug "---------------------------------------------------"
  Debug ""
  
  SetTableAlpha(255)   ; Add Alpha=255 to all Colors
  
  For I = 0 To #X11Col_MaxID
    Debug Str(I) + " : " + Hex( *X11Col\col[I],#PB_Long )
  Next
  
  Debug " "
  ; Here is the way how to use X11Col
  Debug "Aqua = "  + Hex( X11Col(#X11Col_ID_Aqua) ,#PB_Long)
  Debug "Black = " + Hex( X11Col(#X11Col_ID_Black) ,#PB_Long)
  

  Structure TMyColScheme
    Forground.l
    Background.l
    Text.l
  EndStructure
  
  Define MyColorScheme.TMyColScheme
  
  With MyColorScheme
    \Forground = X11Col(#X11Col_ID_Yellow)          ; Forground = X11_Yellow
    \Background = X11Col(#X11Col_ID_LightGray)
    \Text = X11Col(#X11Col_ID_Black)
  EndWith
  
  Debug ""
  Debug "X11ColorID_Check"
  Debug Str(#X11Col_ID_Aqua) + " : " + X11ColID_Check(#X11Col_ID_Aqua)
  Debug Str(-1)  + " : " + X11ColID_Check(-1)
  Debug Str(255) + " : " + X11ColID_Check(255)

 
CompilerEndIf