PYTHON: how to "translate" color codes to HEX colors? [SOLVED]

Discussion in 'Education' started by DrumcodeX, Apr 7, 2024.

  1. DrumcodeX

    DrumcodeX Platinum Record

    Joined:
    Jul 28, 2014
    Messages:
    295
    Likes Received:
    262
    Location:
    Reality (virtual)
    Apparently the Python programming language has sort of its' own and exclusive color coding system, for example: 0.9882353, 0.4745098, 0.039215688 (should be some orange color)

    I'm looking for a way to read out/convert a color code like the one above as HEX color, or RGB, or whatever common color code. Nowadays there are converters for almost anything, but I can't ffs find something for this task.
    Maximum I can find are vague instructions for doing this directly within Python, but I'm no coder, so...
    Despite that, if anyone here could give me clear instructions, maybe a short pre-written python script, I might be able to do this on my own. In case there's no other way of doing that thing.

    The story behind is that I'm evaluating if/how it's possible to customize the GUI of Bitwig Studio. A very essential part of this is to identify the colors.
    Bitwig comes with a .theme file that's obviously written in Python, and all colors there are in that unique format, example below:
    Code:
                        },
                        {
                            class : "widget_theme.color_style",
                            object_id : 59,
                            data :
                            {
                                "name" : "Href Text",
                                "mode" : 0,
                                "absolute_color" :
                                {
                                    type : "color",
                                    data : [0.89411765, 0.12156863, 0.21960784]
                                },
                                "reference_color" : null,
                                "relative_hue" : 0.0,
                                "relative_saturation" : 0.0,
                                "relative_lightness" : 0.0
                            }
                        },
                        {
    In this case the task would be to find out what color "data : [0.89411765, 0.12156863, 0.21960784]" is.

    Any help appreciated, thx.

     
  2.  
  3. Quantised Noise

    Quantised Noise Producer

    Joined:
    Mar 12, 2018
    Messages:
    198
    Likes Received:
    88
    Each value there is a floating point 0.0 to 1.0 value, whereas a hex value is 0 to 255 represented in hex where the format is #RRGGBB

    If you load data into 3 values as R, G and B, then you can use f-strings to print it simply with:

    print(f'#{int(R*255):02X}{int(G*255):02X}{int(B*255):02X}')

    edit: I'll leave the task of reading the JSON format file as something else for you to look up, since that wasn't the scope of your question, but there should be plenty of tutorials on reading JSON online, then you just have R = data[0] G = data[1] B = data[2] ...
     
  4. DrumcodeX

    DrumcodeX Platinum Record

    Joined:
    Jul 28, 2014
    Messages:
    295
    Likes Received:
    262
    Location:
    Reality (virtual)
    Thanks already!
    Ok being somebody with close to zero programming knowledge I still can't really get something going with the infos :no:

    What I tried so far:
    I was using an online python tool (this one >> https://www.online-python.com/ ), pasted the code you provided in and ran it. It didn't work, and I didn't expect it to work anyway because I assume the "source color(s)" need to specified somewhere in the code line you provided (?)
    This is what it spit out:

    [​IMG]

    I tried to implement the "source colors" somwhere in that line, but since I don't really have a clue lol I inserted them sort of randomly.
    As expected it I only came up with errors again:

    [​IMG]

    Additionally I had a quick look at reading JSON, as you mentioned.
    The respective Bitwig file isn't in .json format, it's a .theme file (I'll attach it, zipped since the file type isn't uploadable). Nevertheless I tried throwing it into a JSON reader (this one >> https://jsonformatter.org/json-reader ) but it spit out an error right in line 1:

    [​IMG]

    Same result when renaming "default.theme" to "default.json"
    (didn't expect it would work, but gave it a shot)

    So, as of now I'm still stuck with the issue.
     

    Attached Files:

  5. xorome

    xorome Audiosexual

    Joined:
    Sep 28, 2021
    Messages:
    1,176
    Likes Received:
    860
    Best Answer
    Take the numbers, multiply them by 255 and round them. Stitch the three numbers back together.

    data : [0.89411765, 0.12156863, 0.21960784] ->
    0.89411765 * 255 = 228 red
    0.12156863 * 255 = 31 green
    0.21960784 * 255 = 56 blue

    If you need hex, take the rounded numbers and convert them.

    https://www.binaryhexconverter.com/decimal-to-hex-converter

    data : [0.89411765, 0.12156863, 0.21960784] ->
    0.89411765 * 255 = 228 = e4
    0.12156863 * 255 = 31 = 1f
    0.21960784 * 255 = 56 = 38

    -> e41f38
     
  6. DrumcodeX

    DrumcodeX Platinum Record

    Joined:
    Jul 28, 2014
    Messages:
    295
    Likes Received:
    262
    Location:
    Reality (virtual)
    Holy cow, that's it!! And it's even rather simple :woot:

    All I have to do multiply with 255 to get RGB values, but two days of desperate web searching didn't bring up anything close to that:facepalm:

    Makes it even more stunning that the solution is actually easy. Thank you very much @xorome :bow:
    This drove me nuts for almost two days lol.
     
  7. Quantised Noise

    Quantised Noise Producer

    Joined:
    Mar 12, 2018
    Messages:
    198
    Likes Received:
    88
    Sorry, since you asked specifically for a python solution I assumed you had at least a basic understanding of python...

    The solution as I posted would have resulted in this online-python session:

    [​IMG]
     
  8. DrumcodeX

    DrumcodeX Platinum Record

    Joined:
    Jul 28, 2014
    Messages:
    295
    Likes Received:
    262
    Location:
    Reality (virtual)
    Yea I guess I wasn't clear enough that I was looking for a solution outside python in the first place, sorry.
    For some reason all my web searching drifted right towards having to do this directly in python. So I pretty much expected I would have to do it in python ("In case there's no other way of doing that thing.")

    I'm still thankful for your replies, especially that you posted how your solution has to look like in online-python :bow: It's just a simple thing, but for me it's a nice little practical example to get a bit of an idea how something like this works, so thank you!
     
    Last edited: Apr 8, 2024
Loading...
Loading...