Color wheel math, 2d vector to rgb?

This is what i have.


    # get 2d Vector ,-1<=x&y<=1 and x^2+y^2<=1    # x&y are in a unit circle
    vec                 = sliders["slider4"]
    
    #asign r,g,b to 30,150 270 (degrees) on the wheel
    vecR = Vector( [1,1/3**0.5]     ).normalized()
    vecG = Vector( [-1,1/3**0.5]    ).normalized()
    vecB = Vector( [0,-1]           ).normalized()    
   
    r   = max(3**0.5-(vec-vecR).length,0)/3**0.5
    g   = max(3**0.5-(vec-vecG).length,0)/3**0.5
    b   = max(3**0.5-(vec-vecB).length,0)/3**0.5
    
    
    color = Vector([r,g,b]).normalized()    
    brightness          = to255(  sliders["slider5"].x  )/255
    color *= brightness/max(color) # divide -> dominant part will be 1

For example, how I calculate red part:
Black is the color wheel.
I get the distance to R and math it to range [0,1] over the distance sqrt3…

In game:
http://i.imgur.com/YqzsqjX.gif

Is this acceptable, or is there a serious problem with this?
( do I anger the gods of color ? )

I found the wiki article quite confusing, that is why I tried to do it the stupid way.
I need a bare minimum “color wheel”, doesn’t have to be accurate.

The blend is crude at best.
i - options menu toggle

Attachments

v1.zip (428 KB)

Looks nice! I really like the mothed you make them, but the sliders doesn’t fit the green bars sometimes!

Thank you.
I blame bgui :smiley:

#offtopic
Initial green bar bug was solved 1 sec after the thread was made,
but I also must use the slow 5frame delay widget.move method,as changing widget.position will change other variables for some reason

#onTopic


from PIL import Image


img = Image.new( 'RGB', (255,255), "black") # create a new black image
pixels = img.load() # create the pixel map


def dist(a,b):
    return sum([(a[i]-b[i])**2 for i in range(len(a)) ])**0.5
def avg(a,b):
    return [(a[i]+b[i])/2 for i in range(len(a)) ]
def getColor(vec):     
    
    #asign r,g,b to 30,150 270 (degrees) on the wheel
    vecR = (0.8660, 0.5000)
    vecG = (-0.8660, 0.5000)
    vecB = (0.0000, -1.0000)
    
    
    d   = 3**0.5
    e   = 1
    
    r   = (max(d-dist(vec,vecR),0)/d)**e
    g   = (max(d-dist(vec,vecG),0)/d)**e
    b   = (max(d-dist(vec,vecB),0)/d)**e   
    
    
    color = [r,g,b]
    size  = dist(color,[0,0,0])
    color = [c/size for c in color]
    brightness          = 1
    color = [int(c*brightness*255) for c in color ]
    


    return tuple(color)


 
for i in range(img.size[0]):    # for every pixel:
    for j in range(img.size[1]):




        vec = ((i/255-0.5)*2,(j/255-0.5)*2)
        color = getColor(vec)
        size = dist(vec,[0,0,0])
        if size>1:           
            color = tuple([int(c/size**9) for c in color])
            
        pixels[i,j] = color
       


        
img.save("example.png") 
img.show()



If someone wants to try PILLOW,
http://www.lfd.uci.edu/~gohlke/pythonlibs/


Yeah, that one came useful for me;)