December 04, 2020, 11:02:23 AM

Author Topic: [bb] 2D Colour chooser by _PJ_ [ 1+ years ago ]  (Read 479 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 2D Colour chooser by _PJ_ [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : 2D Colour chooser
Author : _PJ_
Posted : 1+ years ago

Description : A function to select colours, rather than repeat individual RGB commands. The list of colours can be easily amended where necessary. Can be optimised to keep most-used colours near the start of list so the entire Select/Case statement doesn't need to be iterated each time.

Code :
Code: BlitzBasic
  1. Function TxtCol(col$)
  2.  
  3. Select col$
  4.  
  5. Case "white"
  6. RED=225
  7. GREEN=225
  8. BLUE=225
  9.  
  10. Case "brilliant"
  11. RED=255
  12. GREEN=255
  13. BLUE=255
  14.  
  15. Case "black"
  16. RED=0
  17. GREEN=0
  18. BLUE=0
  19.  
  20. Case "silver"
  21. RED=180
  22. GREEN=180
  23. BLUE=195
  24.  
  25. Case "red"
  26. RED=225
  27. GREEN=0
  28. BLUE=0
  29.  
  30. Case "emerald"
  31. RED=0
  32. GREEN=225
  33. BLUE=0
  34.  
  35. Case "blue"
  36. RED=0
  37. GREEN=0
  38. BLUE=225
  39.  
  40. Case "yellow"
  41. RED=225
  42. GREEN=225
  43. BLUE=0
  44.  
  45. Case "magenta"
  46. RED=225
  47. GREEN=0
  48. BLUE=225
  49.  
  50. Case "orange"
  51. RED=195
  52. GREEN=170
  53. BLUE=0
  54.  
  55. Case "brown"
  56. RED=80
  57. GREEN=95
  58. BLUE=0
  59.  
  60. Case "cyan"
  61. RED=0
  62. GREEN=225
  63. BLUE=225
  64.  
  65. Case "crimson"
  66. RED=80
  67. GREEN=0
  68. BLUE=0
  69.  
  70. Case "navy"
  71. RED=0
  72. GREEN=0
  73. BLUE=80
  74.  
  75. Case "green"
  76. RED=0
  77. GREEN=80
  78. BLUE=0
  79.  
  80. Case "light grey"
  81. RED=150
  82. GREEN=150
  83. BLUE=150
  84.  
  85. Case "dark grey"
  86. RED=80
  87. GREEN=80
  88. BLUE=80
  89.  
  90. Default
  91. RED=255
  92. GREEN=255
  93. BLUE=255
  94.  
  95. End Select
  96.  
  97. Color RED,GREEN,BLUE
  98.  
  99. End Function
  100.  
  101. ;Just add more colours as necessary (to improve speed, try to keep most popular colours at the top of the 'Select' list.


Comments :


jhague(Posted 1+ years ago)

 A better approach would be to define a constant for each color (COLOR_WHITE = 0, COLOR_BRILLIANT = 1), then simply index into an array of color values.  This gets rid of the string compares and the entire select structure.


Jeppe Nielsen(Posted 1+ years ago)

 Or do it like this, via constants and without an array:You can use the colour() function like the normal color() command or use it with a color constant:
Code: [Select]
Const color_red=$FF0000
Const color_green=$00FF00
Const color_blue=$0000FF
Const color_yellow=$FFFF00

Function Colour(rgb,g=-1,b=-1)
If g=-1
Color 0,0,rgb
Else
Color rgb,g,b
EndIf
End Function

Colour color_red
Text 10,10,"Red"

Colour color_green
Text 10,30,"Green"

Colour color_blue
Text 10,50,"Blue"

Colour color_yellow
Text 10,70,"Yellow"

Colour 255,255,255
Text 10,90,"White"

MouseWait


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal