nortikin / sverchok

Sverchok
http://nortikin.github.io/sverchok/
GNU General Public License v3.0
2.23k stars 232 forks source link

Node for Color, RGB, HSV, etc #994

Closed enzyme69 closed 7 years ago

enzyme69 commented 7 years ago

I kind of wish Sverchok has node that deals with Color properly. No need to be vectorized yet, just something simple like the AN one.

If not, I will resort to the Script node, but Sverchok Mini Script node is not yet ready, right?

Anyone recalled someone asking the same question in the "Issues" at some point?

Kind of needing to colorize something.

zeffii commented 7 years ago

@enzyme69 use separate issues to ask separate questions please. questions about snlite, please direct them to the snlite thread.

but to answer your main question, a color node is probably way over due :)

enzyme69 commented 7 years ago

screen shot 2016-11-28 at 11 05 50

I think I just screen grab the color node from Animation Nodes above.

Probably Sverchok one will be vectorized and can do other neat things.

You probably have seen me using this node many times in my AN live noding video.

If there is other thing to add, I might sketch it out.

nortikin commented 7 years ago

like that idea. do with color... todo

zeffii commented 7 years ago

parking here: https://docs.python.org/3.5/library/colorsys.html this standard module does all these transforms.

colorsys.rgb_to_yiq(r, g, b)
# Convert the color from RGB coordinates to YIQ coordinates.

colorsys.yiq_to_rgb(y, i, q)
# Convert the color from YIQ coordinates to RGB coordinates.

colorsys.rgb_to_hls(r, g, b)
# Convert the color from RGB coordinates to HLS coordinates.

colorsys.hls_to_rgb(h, l, s)
# Convert the color from HLS coordinates to RGB coordinates.

colorsys.rgb_to_hsv(r, g, b)
# Convert the color from RGB coordinates to HSV coordinates.

colorsys.hsv_to_rgb(h, s, v)
# Convert the color from HSV coordinates to RGB coordinates.
zeffii commented 7 years ago

maybe this needs a new menu Color. with the following nodes

zeffii commented 7 years ago

@nortikin @ly29 @Kosvor2

i'm tempted to add a feature to the StringsSocket socket type where the socket's draw color is defined as an instance property and can be overwritten in a node's sv_init / process functions. For sockets that are essentially duplicating all functionality of a StringsSocket, but that require (for user friendliness) a slight modification to the socket color .

I don't mind doing this..

zeffii commented 7 years ago

https://github.com/nortikin/sverchok/tree/color_nodes_v1

zeffii commented 7 years ago

xolgif

zeffii commented 7 years ago

i'm considering using the Color() class to transfer color data.

what?! :)

zeffii commented 7 years ago

meh, never!

                                       # rgb, etc,                 # components
self.outputs['Colors'].sv_set({'kind': self.selected_mode, 'data': series_vec})
zeffii commented 7 years ago

colzgif

ly29 commented 7 years ago

Nice, would be good with a graphic color picker as well.

enzyme69 commented 7 years ago

Nice one Zeffii, we can now do Sorting and Filtering of RGB colors. Sorting is an interesting one, I reckon.

zeffii commented 7 years ago

we can now do Sorting and Filtering of RGB colors.

hold on.. these nodes aren't functional yet.

zeffii commented 7 years ago

I think sv_get between color(c)->vertices(v) or color(c)->lists(s) will need a conversion step to take the data from the color dict.. likewise, vertices(v)->color(c) , lists(s)->color(c) would also need such a step to transform their data into a dict...

zeffii commented 7 years ago

the problem i have is that FloatVectorProperty won't let us limit min/max per component, this is ok for RGB, HSV, HSL, but will be problematic for YIQ because I and Q have a range between [-1, 1].

ly29 commented 7 years ago

The sane thing would probably to use RGB (or RGBA?) internally but allow different inputs types. Then it would be simpler to use it. Compare with cycles

zeffii commented 7 years ago

@ly29 i'm probably mistaken (but possibly not) I think the cycles nodes for rgb have a dedicated RGBA color ui template.. for the 4 params. there's no RGBA subtype for FloatVectorProperty

image

tempted to really make this atomic and just have a colorwheel node too, and keep it out of the socket.. but undecided, depends on taste ..

ly29 commented 7 years ago

also, two float vector propties and switch when using YIQ? or skip YIQ, is it really that used?

zeffii commented 7 years ago

I am quite happy to ditch YIQ from prominence, and maybe offer a conversion node between spaces instead.

ly29 commented 7 years ago

I want a color socket and that color data is like formatted like vertices. RGB is the format for color values by default but other colospaces are also allowed.

zeffii commented 7 years ago

A color socket is definitely more sane than hijacking the StringsSocket. I tried StringsSocket, it worked but it felt icky.

zeffii commented 7 years ago

i'll make this today, but will hijack the StringsSocket.

ly29 commented 7 years ago

If you use vertex like data I say use vertices otherwise stringssocket. It is the generic data type

zeffii commented 7 years ago

image

zeffii commented 7 years ago

Yellow color socket is just an existing Edges/Polygons (StringsSocket) with a changeable socket-color property.

current limitation - always have RGBA going through these yellow color sockets. and offer a conversion socket

image

image

enzyme69 commented 7 years ago

screen shot 2017-02-19 at 22 06 53

Brain is not so smart tonight, but I know I can import colorsys and convert HSV into RGB. SN Lite is cool, I just don't understand this fully yet.

Vectorization and Nestiness of Sverchok sometimes taken for granted:

"""
in in_hsv v default=(1,0,0) n=1
out out_rgb v 
out out_rgba s 
"""
import colorsys as cs

out_rgb = cs.hsv_to_rgb(in_hsv[0][0], in_hsv[0][1], in_hsv[0][2])

out_rgba = [out_rgb[0], out_rgb[1], out_rgb[2], 1.0]
enzyme69 commented 7 years ago

I am currently studying this example of Vectorize of SN Lite: https://github.com/nortikin/sverchok/issues/1005

enzyme69 commented 7 years ago
"""
in in_hsv v default=(1,0,0) n=1
out out_rgb v 
out out_rgba s 
inject
"""
import colorsys as cs

params = vectorize(in_hsv)

#out_rgb = cs.hsv_to_rgb(in_hsv[0][0], in_hsv[0][1], in_hsv[0][2])
#out_rgba = [out_rgb[0], out_rgb[1], out_rgb[2], 1.0]

# points = [FlowerCurve(*param_set) for param_set in zip(*params)]

out_rgb = [ cs.hsv_to_rgb(*param_set) for param_set in zip(*params) ]
#points = [FlowerCurve(*param_set) for param_set in zip(*params)]

Still wrong, but brute force it....

zeffii commented 7 years ago

image

"""
in hue         s default=1.0 n=1
in saturation  s default=1.0 n=1
in value       s default=1.0 n=1
out out_rgb v 
inject
"""
import colorsys as cs

params = vectorize(parameters)
out_rgb = [cs.hsv_to_rgb(*param_set) for param_set in zip(*params)]
zeffii commented 7 years ago

keeping digging.

enzyme69 commented 7 years ago

I see.. thanks Zeffii. I was heading this way. Vector Input and this param* was too much, but I noticed by simplifying the input as single and this sort of make sense.

enzyme69 commented 7 years ago

I'll save the list comprehension for other time, but I think below works.

"""
in in_hue s d=1.0 n=1
in in_sat s d=1.0 n=1
in in_val s d=1.0 n=1
in in_alpha s d=1.0 n=1
out out_rgb v 
out out_rgba s
inject
"""
import colorsys as cs

params = vectorize(parameters)

#out_rgb = cs.hsv_to_rgb(in_hsv[0][0], in_hsv[0][1], in_hsv[0][2])
#out_rgba = [out_rgb[0], out_rgb[1], out_rgb[2], 1.0]

#print(params[0])

# in case it failed
out_rgb = [1,1,1]
out_rgba = [1,1,1,1]

# this is working
out_rgb = [cs.hsv_to_rgb(*param_set) for param_set in zip(params[0], params[1], params[2])]

out_rgba = []

for rgb in out_rgb:
    r,g,b = rgb
    out_rgba.append([r,g,b,in_alpha])
enzyme69 commented 7 years ago

At least by doing this example, I understand the inject part a bit better. To be continued...

enzyme69 commented 7 years ago

colorsys_005_2017_02_19_11_49.zip

enzyme69 commented 7 years ago

Additional revision. The vectorized Alpha is now used.


"""
in in_hue s d=1.0 n=1
in in_sat s d=1.0 n=1
in in_val s d=1.0 n=1
in in_alpha s d=1.0 n=1
out out_rgb v 
out out_rgba s
inject
"""
import colorsys as cs

params = vectorize(parameters)

#out_rgb = cs.hsv_to_rgb(in_hsv[0][0], in_hsv[0][1], in_hsv[0][2])
#out_rgba = [out_rgb[0], out_rgb[1], out_rgb[2], 1.0]

#print(params[0])

# in case it failed
out_rgb = [1,1,1]
out_rgba = [1,1,1,1]

# this is working
out_rgb = [cs.hsv_to_rgb(*param_set) for param_set in zip(params[0], params[1], params[2])]

#print(params[3])

out_rgba = []

for num, rgb in enumerate(out_rgb):
    r,g,b = rgb
    out_rgba.append([r,g,b,params[3][num]])
zeffii commented 7 years ago
for num, (r, g, b) in enumerate(out_rgb):
    out_rgba.append([r, g, b, params[3][num]])
zeffii commented 7 years ago

i'll do some more to https://github.com/nortikin/sverchok/tree/colornodes

zeffii commented 7 years ago

https://github.com/nortikin/sverchok/pull/1253 pushed to beta nodes for now.

zeffii commented 7 years ago

i haven't fully worked out the use_alpha toggle yet.

f.ex: in the color_out node i want it to be able to drop the alpha channel even if the input contains alpha.. no time atm.

zeffii commented 7 years ago

please continue conversion here. about the Beta Nodes. https://github.com/nortikin/sverchok/pull/1253