Hue to RGB? (Or any range of number to RGB)

This forum is currently in read-only mode.
From the Asset Store
HSV RGB Color Picker and Converter for Construct 3
  • I need to change the color filter of a sprite, based on HUE or even any range of number (i.e. 1-1000).

    I know I can do this with the HUE shader effect, but my netbook doesn't like it, and I'd like to stay away from pixel shaders (for performance)

    How can I get RGB from a range of numbers?

  • Right click on the bar above the color, select use expression, then use rgb(r, g, b) with r, g, and b being values from 0-255.

  • I know I can do that, what I need is to find out the rgb from a hue.

  • Oh, sorry, misunderstood. Not sure how to do that.

  • That are two different demands.

    1) Hue

    Will never stand alone, it is part of some models that represent RGB in another geometry. You will find 3 models, HSV, HSL, HSI (Hue Saturation [Value, Lightness, Intensity]). In case you need to convert from one of the first two models, Python is the easiest way:

    import colorsys
    my_rgb = colorsys.hsv_to_rgb(0.25, 0.5, 0.4)
    r = my_rgb[0] * 255
    g = my_rgb[1] * 255
    b = my_rgb[2] * 255[/code:3prbcqlu]
    
    [i]A more useful implementation[/i]
    
    1. Put the following script as a subevent of 'Start of layout':
    [code:3prbcqlu]import colorsys
    
    class FromHSV(object):
        def __init__(self):
            self.__h = 0
            self.__s = 0
            self.__v = 0
    
        def set_hue(self, hue):
            self.__h = hue
    
        def set_saturation(self, saturation):
            self.__s = saturation
    
        def set_value(self, value):
            self.__v = value
    
        def get_red(self):
            return colorsys.hsv_to_rgb(self.__h, self.__s, self.__v)[0] * 255
    
        def get_green(self):
            return colorsys.hsv_to_rgb(self.__h, self.__s, self.__v)[1] * 255
    
        def get_blue(self):
            return colorsys.hsv_to_rgb(self.__h, self.__s, self.__v)[2] * 255
    
    Convert = FromHSV()[/code:3prbcqlu]
    
    Then, as soon as you have the HSV values, use any of these to fill the class:
    [code:3prbcqlu]Convert.set_hue(yourhuehere)
    Convert.set_saturation(yoursathere)
    Conert.set_value(yourvalhere)[/code:3prbcqlu]
    
    And finally, whereever you need the rgb, use the functions get_red, get_green or get_blue. In the rgb expression it would be:
    [code:3prbcqlu]RGB(Python("Convert.get_red()"), Python("Convert.get_green()"), Python("Convert.get_blue()"))[/code:3prbcqlu]
    
    2) From any range of numbers
    I'm not sure what you mean. Basically, red, green and blue are expressed as values in the range [0, 255]. If you have values in another range, just normalize them and then map them to [0, 255]. For example, v in range [0, 1000] would become v / 1000 * 255, v in range [3, 20] would become (v - 3) / 17 * 255, etc.
  • Thanks, I'll look at that.

    But just to clarify, the Sat & Val are always 100. I just need to retrieve the RGB with the HUE being the only changing variable.

    Is there a simpler way? (preferably without using Python?)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I couldn't think of anything easier than calling a function "hsv_to_rgb"

    If you don't want to use Python, you can always do your own conversion events. Converting between color models involves some math, here is one example code (or adapt the one from the shader):

    http://books.google.com/books?id=fGX8yC-4vXUC&pg=PA304#v=onepage&q&f=false

  • If anyone is interested I figured it out:

    RGB(Max(510*Abs(Cos([ANGLE]))-255, 0), Max(510*Abs(Cos([ANGLE]+120))-255, 0), Max(510*Abs(Cos([ANGLE]+240))-255, 0)) [/code:xnwb0n3t]
    
    Thanks for the help.
  • tulamide your python class works great when setting the values if i set them directly like -

    Convert.set_hue(0.25)

    Convert.set_saturation(0.5)

    Convert.set_value(0.4)

    however when i do this to have EditBox input -

    Convert.set_hue(EditBox.Value)

    Convert.set_saturation(EditBox2.Value)

    Convert.set_value(EditBox3.Value)

    I am getting errors, so i am guessing this might be the wrong way to get EditBox values. Is there a way to make the EditBox inputs work?

  • The edit box only knows text. You access its content with 'Get text' (Editbox.Text). But Python does not autoconvert the text to a number. So use the Python-built-in function float(), e.g.:

    Convert.set_hue(float(EditBox.Text))[/code:2v0yez47]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)