March 01, 2021, 10:43:35 PM

Author Topic: [bb] darken or lighten a color by CloseToPerfect [ 1+ years ago ]  (Read 513 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : darken or lighten a color
Author : CloseToPerfect
Posted : 1+ years ago

Description : If you notice when you use the color picker in paint when defining custom colors you have a slider bar on the right with a light to dark scale.  in the color picker that value is called lum.  the higher the lum value the brighter it is.

This function duplicates that feature.  Enter a rgb value and set the ccl argument to 128 and you have the base value unmodified.  set ccl to a lower number and the color will be darker.  Set it to a higher number and it will be brighter.  It has a optional argument to set if you only want to return the integer value of the number.   Function is documented.

CTP


Code :
Code: BlitzBasic
  1. Function cc_set_color(ccl,ccr,ccg,ccb,ccset=1)
  2. ;usage cc_set_color(ccl,ccr,ccg,ccb,ccset=0)
  3. ;ccl is brightness level 128 is the normal color
  4. ;a lower value will make the color shades darker
  5. ;a hight value will make the color brighter
  6. ;ccset=1 will set the current drawing color
  7. ;and return the integer value of the rgb color
  8. ;ccset=0 will only return the integer value of the rgb
  9. ;completly dark is black, sets colors to 0,0,0 or int 0.
  10. ;completly bright is white, sets colors to 255,255,255 or a int 16777216
  11. ;a grey color 100,100,100 for example will be 100,100,100 if ccl set to 128
  12. ;and will be 99,99,99 when ccl is set to 127
  13. ;or will be 101,101,101 when ccl is set to 129
  14. ;to return a int value usage, integer_varible = cc_set_color(ccl,ccr,ccg,ccb,ccset=0)
  15. ;128 is normal color
  16. ;127-0 darkens
  17. ;128-255 brightens
  18. If ccl<128  
  19.         rs#=Float ccr/127
  20.         gs#=Float ccg/127
  21.         bs#=Float ccb/127
  22.         ccred=ccl*rs#
  23.         ccgreen=ccl*gs#
  24.         ccblue=ccl*bs#
  25. ElseIf ccl>127
  26.         ccl=ccl-128
  27.         rs#=Float (255-ccr)/127
  28.         gs#=Float (255-ccg)/127
  29.         bs#=Float (255-ccb)/127
  30.         ccred=ccr+ccl*rs#
  31.         ccgreen=ccg+ccl*gs#
  32.         ccblue=ccb+ccl*bs#
  33. EndIf
  34. If ccset=1 Then Color ccred,ccgreen,ccblue
  35. Return ccblue Or (ccgreen Shl 8) Or (ccred Shl 16)
  36. End Function
  37.  
  38.  
  39. ;example usage
  40. Graphics 510,510,16,2
  41. For i=50 To 250
  42. cc_set_color(i,50,90,200)
  43. Oval i/1.25,i/3,255-i,255-i
  44. cc_set_color(i,205,50,70)
  45. Oval 220+i/1.25,i/3,255-i,255-i
  46. cc_set_color(i,55,127,155)
  47. Oval i/1.25,220+i/3,255-i,255-i
  48. cc_set_color(i,250,90,200)
  49. Oval 220+i/1.25,220+i/3,255-i,255-i
  50. Flip
  51. Next
  52. For i=0 To 255
  53. cc_set_color(i,90,30,150)
  54. Line 0,i,GraphicsWidth(),i
  55. Line 0,510-i,GraphicsWidth(),510-i
  56. Flip
  57. Next
  58. For i=0 To 255
  59. cc_set_color(255-i,90,130,50)
  60. Line 510-i,0,510-i,GraphicsHeight()
  61. cc_set_color(255-i,90,130,50)
  62. Line i,0,i,GraphicsHeight()
  63. Flip
  64. Next
  65. For i = 0 To 255
  66. Color 0,0,0
  67. Rect 255-i,255-i,i*2,i*2,0
  68. Flip
  69. Next
  70.  
  71. For i=50 To 250
  72. cc_set_color(i/2,250,90,200)
  73. Oval i/1.25,i/3,255-i,255-i
  74. Next
  75. image=CreateImage(255,255)
  76. GrabImage image,0,0
  77. x=110:y=-50
  78. height = 0
  79. dir=0
  80. Color 255,255,255
  81. Repeat
  82. Cls
  83. DrawBlock image,x,y
  84. If dir = 0 Then y=y+g# Else y=y-g#
  85. If dir=0 And y>285 Then dir=1
  86. If dir=1 And g#<0 Then dir=0
  87. If dir = 0 Then g#=g#+.1
  88. If dir = 1 Then g#=g#-.15
  89. Text 0,0,"hit esc to quit"
  90. Flip
  91. Until KeyHit(1)


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal