Notifications
Clear all

[Closed] convert HSV to RGB

Hi

I need help convert HSV to RGB. Is it any fast method in maxscript to do that, because unfortunately i can’t find nothing or I need use some key code for this transformation, because i have found few keys code from python or java but i am not understand very well to those languages . So has anybody idea how i can solve my problem :)? Has anybody this conversion already coded :)?

cheers

11 Replies

So if will be somebody else solve this problem in future here is my solution

HSV_col = [38,30,250]                                                -- input HSV
  fn HSVtoRGB =
      (
          h = hsv_col[1] / 255 as float
          s = hsv_col[2] / 255 as float
          v = hsv_col[3] / 255 as float
          if ( S == 0 ) then 
              ( 
                  R = V * 255 
                  G = V * 255
                  B = V * 255
              )
              else
                  (
                      var_h = H * 6
                      if ( var_h == 6 ) do ( var_h = 0 )
                      var_i = (( var_h ) as integer)
                      var_1 = V * ( 1 - S )
                      var_2 = V * ( 1 - S * ( var_h - var_i ) )
                      var_3 = V * ( 1 - S * ( 1 - ( var_h - var_i ) ) )
                  )
          if ( var_i == 0 ) then 
                  ( 
                      var_r = V
                      var_g = var_3
                      var_b = var_1
                  )
             else 
                  (
                      if ( var_i == 1 ) then 
                      ( 
                          var_r = var_2
                          var_g = V
                          var_b = var_1 
                      )
                          else
                              (
                                  if ( var_i == 2 ) then 
                                  (
                                      var_r = var_1
                                      var_g = V 
                                      var_b = var_3 
                                  )
                                      else 
                                          (
                                              if ( var_i == 3 ) then 
                                              (
                                                  var_r = var_1 
                                                  var_g = var_2 
                                                   var_b = V     
                                              )
                                                  else 
                                                      (
                                                          if ( var_i == 4 ) then 
                                                          ( 
                                                              var_r = var_3
                                                              var_g = var_1
                                                              var_b = V
                                                              )
                                                              else
                                                                  ( 
                                                                      var_r = V     
                                                                      var_g = var_1 
                                                                      var_b = var_2 
                                                                  )
                                                      )
                                          )
                              )
                  )
          R = ceil (var_r * 255)
          G = ceil (var_g * 255)
          B = ceil (var_b * 255)
          RGB_col = [R,G,B]                                                -- HSV converted into RGB
      )
  HSVtoRGB ()

MAX color value has h, s, and v properties. If you set them with HSV, does it not return right numbers in R,G, and B?

Hi DenisT

I though the same but from some reason I don’t understand why it doesn’t work this way which is for me curious, or maybe i did something wrong, but i am almost sure I tested it correctly. But if anybody find I am wrong I am happy to see why, because my solution is not very smart and i just rewrite some code which was written for C+

I didn’t have a chance to check it yesterday, but as I thought there is no problem to convert HSV to RGB using of color value:

fn HSV_to_RGB hsv = 
(
	rgb = black -- it might be any color
	rgb.v = hsv.z
	rgb.s = hsv.y
	rgb.h = hsv.x
	rgb as point3
)
HSV_to_RGB [38,30,250]

the order of the setting of HSV is important. It has to be V, S, and H.

Thanks Denis. I am happy this works definitely better than my. I just don’t fully understand what you have done . For example how you can input anything into rgb . From what i know it is read only variable. So i am little confused

probably my local variable “rgb” was confused you. It might be any name. Change “rgb” to “col” if you want. It’s just a local name for color variable.

When I updated the Coolpicker color picker years ago, there was c++ code that would do that for both 8 bit color values, and for floating point values as well. It is currently open source of course, and can be found on maxplugins.de I believe.

Yup, Chris is right, you can get the latest version of the source code here:
http://source.maxplugins.de/colorpicker/CoolPicker_v2.3.1_source.zip

Dave

Here’s my old model translated directly from the pegtop delphi and adobe blendmode documents:

fn vToC valueVal =  --accepts a point3 value, returns an equivalent color value
 (
 	return [(valueVal.x*255 as integer), (valueVal.y*255 as integer), (valueVal.z*255 as integer)] as color
 )
 fn cToV colorVal =  --accepts a color value, returns an equivalent point3 value
   (
   	tempVal = colorVal as point3
   	return [(tempVal.x/255), (tempVal.y/255), (tempVal.z/255)]
   
   )
   fn HSVtoRGB hsvVal =  --accepts an HSV model point3 value, returns an equivalent RGB model point3 value
     (
     	if hsvVal.y == 0 then
     	(
     		return vToC [hsvVal.z, hsvVal.z, hsvVal.z]
     	)
     	else
     	(
     		testH = hsvVal.x*6
     		if testH == 6 then
     		(
     			testH = 0
     		)
     		var_i = testH as int
     		var_1 = hsvVal.z*(1-hsvVal.y)
     		var_2 = hsvVal.z*(1-hsvVal.y*(testH-var_i))
     		var_3 = hsvVal.z*(1-hsvVal.y*(1-(testH-var_i)))
     		case var_i of
     		(
     			0: return [hsvVal.z, var_3, var_1]
     			1: return [var_2, hsvVal.z, var_1]
     			2: return [var_1, hsvVal.z, var_3]
     			3: return [var_1, var_2, hsvVal.z]
     			4: return [var_3, var_1, hsvVal.z]
     			default: return [hsvVal.z, var_1, var_2]
     		)
     	)
     )

This accepts and returns scaled point3 float values from 0-1, so some conversions may be necessary depending on what you’re interfacing it with.

Page 1 / 2