Reading color from materials and change its format to ARGB/RGB

Hello

when i try to read the color parameter of materials:

the output is as you see, i need to change it to RGB to parse it, is there a way to do that?

Use Material.Color node to get the color and then Color.Components to get the rgba

3 Likes

For anyone curious beyond this for “Name” of the color.

7 Likes

Love this @pyXam :star_struck:

On reflection / building of this myself i found the colors that the Color API was returning to be to “Flavoursum” for my liking…

Here is a revised approach using a dictionary instead to return those Dull color names i enjoy naming things…

def get_basic_color_name(rgb_list):
    color_names = []
    for rgb in rgb_list:
        r, g, b = rgb
        colors = {
            "Red": (255, 0, 0),
            "Green": (0, 255, 0),
            "Blue": (0, 0, 255),
            "Yellow": (255, 255, 0),
            "Orange": (255, 165, 0),
            "Purple": (128, 0, 128),
            "Brown": (165, 42, 42),
            "Gray": (128, 128, 128),
            "Magenta": (255, 0, 255),
            "Black": (0, 0, 0),
            "Cyan": (0, 255, 255),
            "Pink": (255, 192, 203),
        }

        def distance(c1, c2):
            (r1, g1, b1) = c1
            (r2, g2, b2) = c2
            return (r1 - r2) ** 2 + (g1 - g2) ** 2 + (b1 - b2) ** 2

        closest_color = min(colors.items(), key=lambda item: distance(item[1], (r, g, b)))
        color_names.append(closest_color[0])
    return color_names

OUT = get_basic_color_name(IN[0])

Or for anyone wanting to use only a python solution to the web request i originally posted.
In [1] is a list of lists of colors in the order R,G,B

import urllib.request
import json

def get_color_info(rgb_list):
    color_names = []
    for rgb in rgb_list:
        r, g, b = rgb
        url = f"http://www.thecolorapi.com/id?rgb=rgb({r},{g},{b})"
        with urllib.request.urlopen(url) as response:
            data = response.read()
            color_data = json.loads(data)
            color_names.append(color_data['name']['value'])
    return color_names

OUT = get_color_info(IN[1])
1 Like