January 15, 2021, 05:24:31 PM

Author Topic: [bb] RGB to HSB and back by jfk EO-11110 [ 1+ years ago ]  (Read 398 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] RGB to HSB and back by jfk EO-11110 [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : RGB to HSB and back
Author : jfk EO-11110
Posted : 1+ years ago

Description : RGB2HSB and HSB2RGB - can be used for altering Hue, Saturation or Brightness (Luminance) independently in an easy way. Then after editing you simply convert it back to RGB.

Code :
Code: BlitzBasic
  1. ; sorry for the Gotos :P
  2. Graphics 640,480,32,2
  3. SetBuffer BackBuffer()
  4.  
  5. Global sat#,lumin#,hue#
  6.  
  7. ; test demo
  8. While KeyDown(1)=0
  9.  sat#=0
  10.  lumin#=0
  11.  hue#=0
  12.  r=Rand(255)
  13.  g=Rand(255)
  14.  b=Rand(255)
  15.  Color r,g,b
  16.  Cls
  17.  Rect 100,100,100,100,1 ; draw in original rgb
  18.  rgb2hsb(rgb(r,g,b))    ; convert to hsb
  19.  rgb=hsb2rgb(hue,sat,lumin)  ; convert back to rgb
  20.  Color getRed(rgb),getGreen(rgb),getBlue(rgb)  ; draw again
  21.  Rect 100,220,100,100,1
  22.  
  23.  Locate 0,0
  24.  Print "R: "+r
  25.  Print "G: "+g
  26.  Print "B: "+b
  27.  Print
  28.  Print "Hue:   "+hue
  29.  Print "Sat:   "+sat
  30.  Print "Lumin: "+lumin
  31.  Print
  32.  Print "Convert back"
  33.  Print "R2: "+getRed(rgb)
  34.  Print "G2: "+getGreen(rgb)
  35.  Print "B2: "+getBlue(rgb)
  36.  Locate 0,GraphicsHeight()-50
  37.  Print "Press a key to continue, Esc to exit"
  38.  Flip
  39.  WaitKey()
  40.  
  41. Wend
  42. End
  43.  
  44.  
  45. Function hsb2rgb(h#,s#,l#) ; takes hue(0 to 360), saturation (0 to 1.0) and luminance (0 to 1.0)
  46.  Local i,f#,p#,q#,t#,r#,g#,b#
  47.  If s=0
  48.   r=l*255.0:g=l*255.0:b=l*255.0
  49.  Else
  50.   h=h/60.0
  51.   i=Floor(h)
  52.   f=h-i
  53.   p=l*(1.0-s)
  54.   q=l*(1.0-s*f)
  55.   t=l*(1.0-s*(1.0-f))
  56.   Select i
  57.    Case 0
  58.     r=l
  59.     g=t
  60.     b=p
  61.    Case 1
  62.     r=q
  63.     g=l
  64.     b=p
  65.    Case 2
  66.     r=p
  67.     g=l
  68.     b=t
  69.    Case 3
  70.     r=p
  71.     b=l
  72.     g=q
  73.    Case 4
  74.     r=t
  75.     b=l
  76.     g=p
  77.    Default
  78.     r=l
  79.     g=p
  80.     b=q
  81.   End Select
  82.   r=r*255
  83.   g=g*255
  84.   b=b*255
  85.  EndIf
  86.  If r<0 Then r=0
  87.  If r>255 Then r=255
  88.  If g<0 Then g=0
  89.  If g>255 Then g=255
  90.  If b<0 Then b=0
  91.  If b>255 Then b=255
  92.  rgb=(r Shl 16)Or(g Shl 8)Or b
  93.  Return rgb
  94. End Function
  95.  
  96.  
  97.  
  98. Function rgb2hsb(rgb) ; takes 24 bit rgb color, returns (global) hue(0 to 360), saturation(0 to 1.0) and luminance(0 to 1.0)
  99.  rgb=rgb And $FFFFFF
  100.  r#=(rgb Shr 16) And $FF
  101.  g#=(rgb Shr 8) And $FF
  102.  b#=rgb And $FF
  103.  my_min#=min#(r,g)
  104.  my_max#=max#(r,g)
  105.  my_min#=min#(my_min#,b)
  106.  my_max#=max#(my_max#,b)
  107.  delta#=(my_max#-my_min#)
  108.  If my_max#<>0
  109.   sat#=delta#/my_max#
  110.   lumin#=my_max#/255.0
  111.   If delta<>0
  112.    If r=my_max Then
  113.     hue=(g-b)/delta
  114.     Goto okii
  115.    EndIf
  116.    If g=my_max Then
  117.     hue=2+((b-r)/delta)
  118.     Goto okii
  119.    EndIf
  120.    hue=4+((r-g)/delta)
  121.    .okii
  122.   EndIf
  123.  EndIf
  124.  hue=hue*60.0
  125.  If hue<0 Then hue=hue+360.0
  126. End Function
  127.  
  128.  
  129.  
  130. Function rgb(r,g,b)
  131.  Return (r Shl 16) Or (g Shl 8) Or b
  132. End Function
  133.  
  134. Function getRed(rgb)
  135.  Return (rgb And $FF0000) Shr 16
  136. End Function
  137.  
  138. Function getGreen(rgb)
  139.  Return (rgb And $FF00) Shr 8
  140. End Function
  141.  
  142. Function getBlue(rgb)
  143.  Return (rgb And $FF)
  144. End Function
  145.  
  146. Function min#(v1#,v2#)
  147.  If v1<v2 Then
  148.   Return v1
  149.  Else
  150.   Return v2
  151.  EndIf
  152. End Function
  153.  
  154. Function max#(v1#,v2#)
  155.  If v1>v2 Then
  156.   Return v1
  157.  Else
  158.   Return v2
  159.  EndIf
  160. End Function


Comments :


big10p(Posted 1+ years ago)

 As requested, here are my versions.I converted these from C code I found in a Foley & Van Dam graphics book. However, the original C code uses double precision floats - which we don't have - so there will probably be a small amount of inaccuracy sometimes. Not much I can do about that. :)
Code: [Select]
; RGB/HSL function return values.
Global result_r#, result_g#, result_b#
Global result_h#, result_s#, result_l#

;
; Converts an RGB color to HSL.
;
; Params:
; ir,ig,ib - Color's red, green and blue components (0-255).
;
; Returns:
; The converted color's HSL values via the following globals:
; result_h - Hue component (0-360).
; result_s - Saturation component (0-1).
; result_l - Luminance component (0-1).
;
Function rgb_to_hsl(ir%, ig%, ib%)

; Scale RGB down to unit range (0-1).
r# = ir/255.0
g# = ig/255.0
b# = ib/255.0

If r > g
max_is = 0
max_color# = r
min_color# = g
Else
max_is = 1
max_color# = g
min_color# = r
EndIf

If b > max_color
max_is = 2
max_color = b
ElseIf b < min_color
min_color = b
EndIf

; Luminance.
result_l = (max_color + min_color) / 2.0

If max_color = min_color
; Color is grey.
result_s = 0
;result_h = 0
Else
delta# = (max_color - min_color)

; Saturation.
If result_l < 0.5
result_s = delta / (max_color + min_color)
Else
result_s = delta / (2.0 - max_color - min_color)
EndIf

; Hue.
Select max_is
Case 0
; Red.
result_h = (g - b) / delta
Case 1
; Green.
result_h = 2.0 + (b - r) / delta
Case 2
; Blue.
result_h = 4.0 + (r - g) / delta
End Select

result_h = result_h * 60.0
If result_h < 0 Then result_h = result_h + 360.0
EndIf

End Function


;
; Converts an HSL color to RGB.
;
; Params:
; h,s,l - Color's hue(0-360), luminance(0-1) and saturation(0-1) components.
;
; Returns:
; The converted color's RGB values via the following globals:
; result_r - Hue component (0-255).
; result_g - Saturation component (0-255).
; result_b - Luminance component (0-255).
;
Function hsl_to_rgb(h#, s#, l#)

If s = 0
result_r# = l
result_g# = l
result_b# = l
Else
If l < 0.5
temp2# = l * (1.0 + s)
Else
temp2# = (l + s) - (l * s)
EndIf
temp1# = 2.0 * l - temp2

h = h / 360.0

rtemp3# = h + 1.0 / 3.0
If rtemp3 < 0
rtemp3 = rtemp3 + 1.0
ElseIf rtemp3 > 1
rtemp3 = rtemp3 - 1.0
EndIf

gtemp3# = h
If gtemp3 < 0
gtemp3 = gtemp3 + 1.0
ElseIf gtemp3 > 1
gtemp3 = gtemp3 - 1.0
EndIf

btemp3# = h - 1.0 / 3.0
If btemp3 < 0
btemp3 = btemp3 + 1.0
ElseIf btemp3 > 1
btemp3 = btemp3 - 1.0
EndIf

; Set red.
If (6.0 * rtemp3) < 1
result_r# = temp1 + (temp2 - temp1) * 6.0 * rtemp3
ElseIf (2.0 * rtemp3) < 1
result_r# = temp2
ElseIf (3.0 * rtemp3) < 2
result_r# = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - rtemp3) * 6.0
Else
result_r# = temp1
EndIf

; Set green.
If (6.0 * gtemp3) < 1
result_g# = temp1 + (temp2 - temp1) * 6.0 * gtemp3
ElseIf (2.0 * gtemp3) < 1
result_g# = temp2
ElseIf (3.0 * gtemp3) < 2
result_g# = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - gtemp3) * 6.0
Else
result_g# = temp1
EndIf

; Set blue.
If (6.0 * btemp3) < 1
result_b# = temp1 + (temp2 - temp1) * 6.0 * btemp3
ElseIf (2.0 * btemp3) < 1
result_b# = temp2
ElseIf (3.0 * btemp3) < 2
result_b# = temp1 + (temp2 - temp1) * ((2.0 / 3.0) - btemp3) * 6.0
Else
result_b# = temp1
EndIf
EndIf

; Scale RGB back to 0-255 range.
; Remove this if you want to keep RGB in the range 0-1!
result_r = result_r * 255
result_g = result_g * 255
result_b = result_b * 255

End Function



jfk EO-11110(Posted 1+ years ago)

 thanks.


GW(Posted 1+ years ago)

 . [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal