pls help how to make rainbow color c++

big_ping

New member
Joined
Feb 7, 2018
Messages
2
Reaction score
0
Hello, i want to make rainbow code for all colors but this code gives me only two colors purple and green can someone post little explanation here thanks. 

this is my code 


static int Hex = 0xFFFF00FF;
if (Hex != 0xFFFFFFFF)
Hex += 0xFFFF00FF;

here im using RGBA, A=alpha last one FF is opacity don't change that
 

big_ping

New member
Joined
Feb 7, 2018
Messages
2
Reaction score
0
user88 said:
Code:
#define RandomColor   D3DCOLOR_XRGB(rand() % 255, rand() % 255, rand() % 255)

thanks  :not bad: that helped btw you know how to set delay between these it changes so fast ?
 

user88

Well-known member
Joined
Jun 29, 2017
Messages
426
Reaction score
165
Location
LINK CLEO DICE HACK HERE!
Dword tick

If(!tick)
tick = gettickcount()

If(gettickccount() < tick - YOURDRLAY)
{
tick = 0
Use color
}


Written on phone lolz
 

monday

Expert
Joined
Jun 23, 2014
Messages
1,127
Solutions
1
Reaction score
158
I guess using random numbers would make it chaotic, you could create few loops and modify variables for red, green and blue.
Type "color picker" in google and notice how these 3 values change when you move the slider between red and blue, it's basically like this:
1. 255, 0, 0
2. 255, 255, 0
3. 0, 255, 0
4. 0, 255, 255
5. 0, 0, 255
6. 255, 0, 255
7. 255, 0, 0 (whole cycle, it's the same as 1st step)

But all happening gradually by adding/substracting small value from the variable

Or create a function looping from 1 to 1530 (because that the number of possible colours from 255*6) and create a function takes into account 6 sections mentioned above and basing on how close it is to the begining and end of certain sections return an appropriate color.
Let's say that the number color between 1 and 1530 is 277 which is in the second section of the 6 color sections. The calculation would have to be like this:
value = 277
index = value / 255
section = listOfSections[index] // each section holds 3 values for RGB, like: 255,0,0
nextSection = listOfSections[index + 1]

//section is an array of 3 ints for RGB values:  255,255,0
//nextSection is an array of 3 ints for RGB values:  0,255,0

howFarOverTheSectionBorderIsValue = 277 % 255 // value modulo 255
redValue = abs(float(section[0] - nextSection[0]) / 255.0 * howFarOverTheSectionBorderIsValue)  //it has to be absolute

greenValue = abs(float(section[1] - nextSection[1]) / 255.0 * howFarOverTheSectionBorderIsValue)    //it has to be absolute

blueValue = abs(float(section[2] - nextSection[2]) / 255.0 * howFarOverTheSectionBorderIsValue)  //it has to be absolute

No matter which way you choose it's not a trivial matter I think, the idea behind above example is to just create 1 function and avoid using multiple loops.


Here's a working example but from python. It's very similar to the example I posted above but it lacked a check for next section to make sure it jumps back to the first one after reaching the last one instead of going over the limit. It also uses [1,0,1] mapping instead of [255,0,255] and doesn't have some useless maths I posted before. That's the part worth checking if you're planning to recreate it in C++:
[shcode=python]
sectionList = [
    [1, 0, 0],
    [1, 1, 0],
    [0, 1, 0],
    [0, 1, 1],
    [0, 0, 1],
    [1, 0, 1],
    ]
    
for i in range(255*6):
    section = sectionList[i/255] # gets the appropriate section like [1,0,0]
    nextSectionIndex = i/255 + 1 if i/255 + 1 < len(sectionList) else 0 # if index of the next section is not lower than the size of sectionList then use the first (0) index
    nextSection = sectionList[nextSectionIndex] # gets the next section like [1,1,0]

    rgb = [0,0,0] # initialize list for rgb
    for j in range(3): # loop through 3 colors
        if section[j] == nextSection[j]: # if both corresponding sections have the same values then just use 0 or 255 for this color depending on their value
            rgb[j] = section[j] * 255
        elif section[j] > nextSection[j]:
            rgb[j] = 255 - (i % 255)
        else:
            rgb[j] = i % 255
    
    DrawCircle(rgb[0],rgb[1],rgb[2],-700+i,0)[/shcode]

[shcode=python]import turtle

#initial setup
t = turtle.Turtle(shape="turtle")
turtle.colormode(255)
t.speed(0)
x = 10
y = 10


def DrawCircle(r,g,b,x,y):
    t.penup()
    t.setposition(x, y)
    t.pendown()
    t.pencolor(r,g,b)
    t.circle(30)

sectionList = [
    [1, 0, 0],
    [1, 1, 0],
    [0, 1, 0],
    [0, 1, 1],
    [0, 0, 1],
    [1, 0, 1],
    ]
    
for i in range(255*6):
    section = sectionList[i/255]
    nextSectionIndex = i/255 + 1 if i/255 + 1 < len(sectionList) else 0
    nextSection = sectionList[nextSectionIndex]

    rgb = [0,0,0]
    for j in range(3):
        if section[j] == nextSection[j]:
            rgb[j] = section[j] * 255
        elif section[j] > nextSection[j]:
            rgb[j] = 255 - (i % 255)
        else:
            rgb[j] = i % 255
    
    DrawCircle(rgb[0],rgb[1],rgb[2],-700+i,0)
    
    #if i % 5 == 0:
        #DrawCircle(rgb[0],rgb[1],rgb[2],-700+i,0)



 
turtle.getscreen()._root.mainloop()


[/shcode]


Here's the result of the above program
[img=800x48]https://i.imgur.com/pDuW82B.png[/img]


Btw I get an impression that it still doesn't represent a rainbow like it should, green seems to have too much significance when compared to colours like yellow. Maybe that's because it's based on the Red-Green-Blue instead of Red-Yellow-Blue...

Now when I look at it I see the section list is not necessary, theres a clear pattern which could be coded appropriately:
inc 2
dec 1
inc 3
dec 2
inc 1
dec 3
inc 2

On the other side using section list would make it easy to create custom patterns of desired colors, like police style using Red and Blue only or more complex ones with various colors. Also without the section list, using that pattern it would be required to go through a series of calculations up to the index of desired colour every time the function to get a color is called which isn't the case with sections data involved...

Here's another way which uses HSV (hue, saturation, value) conversion to RGB.

But still it should be more like this: Image

If you take a look at both rainbow examples above the first one has cyan color in the middle and the second one (proper one?) has green/yellow in the middle.

Here is a Red-Yellow-Blue based method with conversion to RGB so it can be used with functions that accept RGB only. Here's an example of the colors produced by it. Trying to comprehend that conversion itself is a bit like trying to manage 3 loops, each with 3 sets of 3 arrays, each array with 3 strings having 3 letters. It's easy to get lost in it but I added comments within the code and attempted to explain what's going on using real life examples rather than formulas, it's a mixture of both actually.

It looks better in my opinion but lacks some bright colors like cyan or pink. Maybe producing RYB based distribution of pure RGB-produced colors would be more appealing
 
Top