0

I want to have a gradient with rainbow colors.

I have numeric values, the maximum value is the color Red, and minimum value is the color Blue. I try to find an algorithm to have proportional colors, i.e the half will be Green, between half and max will be Yellow or Orange....

I begin with this : Get color value from numeric values But my function is not correct, if someone can help me... I try with HSV, RGB ... But I am more WebDesigner than Developper.. Thanks !

EDIT -----------------

This help ! Thanks a lot, I was sure that Value must be variable, but only with the Hue I get the right thing...

$h = ($value * 360) / $max;

$color = HSV_TO_RGB($h / 360, 1, 1);

First time, I forgot to divide by 360 to have a value between 0-1... I am an idiot. The HSV_TO_RGB function can be easily found on Web (hsv to rgb php in Google). I get the right colors now, using a boucle.

Thanks a lot.

Community
  • 1
  • 1
Julian T
  • 35
  • 6
  • 1
    Show your code. We're not going to write the function for you - we're here to help, not do you job. – Marc B Jul 25 '11 at 14:22

2 Answers2

0

I found this tutorial to be extremely useful for creating a color palette and understanding how rainbows are generally generated. I reccommend to read it thoroughly, to understand how to create both repeating and non-repeating color cycles of arbitrary length.

The javascript was easily translated to OpenCV Python as follows. The makeColorGradient generates the list of RGB colors of specified count. The rest of the code just tests it and displays the gradient nicely. Again refer to the tutorial for cool gradient types and values for the parameters. =)

import math
import cv2
import numpy as np

def makeColorGradient(freq1, freq2, freq3,
                      phase1, phase2, phase3,
                      center=128, width=127, count=50):

    colors = []
    for i in range(count):
        red = int(math.sin(freq1*i + phase1) * width + center);
        grn = int(math.sin(freq2*i + phase2) * width + center);
        blu = int(math.sin(freq3*i + phase3) * width + center);
        #document.write( '<font color="' + RGB2Color(red,grn,blu) + '">&#9608;</font>');
        colors.append((red,grn,blu))
    return colors

def main():
    freq = 2.4 #non repeating color set
    #freq = 0.3 #nice happy rainbow =)
    phases = [0,2,4]
    count = 67 #number of colors to generate
    colors = makeColorGradient(freq,freq,freq,
                               phases[0],phases[1],phases[2],
                               count=count)
    winname = 'Color Gradient count='+str(count)
    cv2.namedWindow(winname)

    w = 800
    h = 100
    canvas = np.zeros((h,w,3),np.uint8)
    linspace = np.linspace(0,w,count,endpoint=True)
    linspace = map(int, linspace)
    for i in range(count-1):
        r1 = (linspace[i], 0)
        r2 = (linspace[i+1], h)
        color = colors[i]
        cv2.rectangle(canvas, r1, r2, color, thickness=cv2.cv.CV_FILLED)
    cv2.imshow(winname, canvas)

    #keep window open till escape key pressed
    while(1):
        if(cv2.waitKey(15) == 27):
            break

if __name__ == '__main__':
    main()
    print 'done'
craastad
  • 6,222
  • 5
  • 32
  • 46
0

Basicaly what you will need is a HSV to RGB function with fixed Value and Saturation (let's say 100 for saturation and value) and only change the Hue between 0 (your min value) and 360 (your max value). You can find many hsv2rgb php implementation over the network and i think that should do the trick.

hope this help

malko
  • 2,292
  • 18
  • 26