Generating an array of colors (ColorBetween related)
#1

Quote:

Click the picture to see an explanation.
I also went to stackoverflow, but nobody is helping out much... I explained the situation a lot better there with test results and stuff.

What I want to do is generate an array of colors from colors provided in the generate function. The Color* functions are in the other post (click-the-pic).
Reply
#2

I looked into this issue for you and came up with a solution you can set the start and end colors to anything you want and also specify how many color gradients you want in between.

Method source: http://www.exorithm.com/algorithm/view/create_gradient

pawn Код:
// Example CreateGradient(colors, 100, 0xFF0000, 0x00FF00);
// Red to Green
#define RGBA(%0,%1,%2,%3) ((((%0) & 0xFF) << 24) | (((%1) & 0xFF) << 16) | (((%2) & 0xFF) << 8) | (((%3) & 0xFF) << 0))
CreateGradient(colors[], size, start_color, end_color)
{
    new red, green, blue, Float:start_r, Float:start_g, Float:start_b, Float:end_r, Float:end_g, Float:end_b;
   
    start_r = float(((start_color >> 16) & 0xFF));
    start_g = float(((start_color >> 8) & 0xFF));
    start_b = float(((start_color) & 0xFF));
   
    end_r = float(((end_color >> 16) & 0xFF));
    end_g = float(((end_color >> 8) & 0xFF));
    end_b = float(((end_color) & 0xFF));

    for(new i=0; i < size; i++)
    {
        red = floatround(start_r - (start_r-end_r) * (float(i) / (float(size-1))));
        green = floatround(start_g - (start_g-end_g) * (float(i) / (float(size-1))));
        blue = floatround(start_b - (start_b-end_b) * (float(i) / (float(size-1))));
        colors[i] = RGBA(red, green, blue, 0xFF);
    }
    return 1;
}
Reply
#3

I already have that. What I was trying to do was create a gradient from multiple colors not only two.
Reply
#4

You would need to explain that more or give some visuals of expected results how is anyone to know what you want exactly?
Reply
#5

I want the GenerateGradient function to work how I thought it should.
pawn Код:
new Array[263] = GenerateGradient([List of colors to interpolate through here.]);
That is supposed to go through each argument and return the gradient of all of them.

EDIT: I was just thinking, even if we do get the cells to fill in there will be some (the remainder of the division) left over. So we also need a modulus in there for the last color.
Reply
#6

pawn Код:
enum E_COLOUR_STOPS
{
    CS_COLOUR,
    CS_PERCENT
}

CreateMultiGradient(const colour_stops[][E_COLOUR_STOPS], colours[], stops = sizeof colour_stops, size = sizeof colours)
{
    if (size < stops) {
        return printf("You need at least %d slots in your resulting array", stops);
    }

    new
        offset = 1,
        total
    ;

    CreateGradient(colours, colour_stops[0][CS_COLOUR], colour_stops[0][CS_COLOUR], 1);

    for (new i = 1; i != stops; ++i) {
        total = floatround(size * (colour_stops[i][CS_PERCENT] * 0.01));
   
        CreateGradient(colours, colour_stops[i - 1][CS_COLOUR], colour_stops[i][CS_COLOUR], total, offset);

        offset += total;
    }
   
    return 1;
}

#define RGBA(%0,%1,%2,%3) ((((%0) & 0xFF) << 24) | (((%1) & 0xFF) << 16) | (((%2) & 0xFF) << 8) | (((%3) & 0xFF) << 0))
CreateGradient(colors[], start_color, end_color, total = sizeof colors, offset = 0)
{
    new red, green, blue, Float:start_r, Float:start_g, Float:start_b, Float:end_r, Float:end_g, Float:end_b;
   
    start_r = float(((start_color >> 16) & 0xFF));
    start_g = float(((start_color >> 8) & 0xFF));
    start_b = float(((start_color) & 0xFF));
   
    end_r = float(((end_color >> 16) & 0xFF));
    end_g = float(((end_color >> 8) & 0xFF));
    end_b = float(((end_color) & 0xFF));

    for(new i = 0; i < total - offset; i++)
    {
        red = floatround(start_r - (start_r-end_r) * (float(i) / (float(total-1))));
        green = floatround(start_g - (start_g-end_g) * (float(i) / (float(total-1))));
        blue = floatround(start_b - (start_b-end_b) * (float(i) / (float(total-1))));
        colors[offset + i] = RGBA(red, green, blue, 0xFF);
    }
    return 1;
}

public OnGameModeInit()
{
    new
        arr[26],
        stops[3][E_COLOUR_STOPS] = {
            { 0xBADA55, 0 },
            { 0x000000, 75 },
            { 0xBADA55, 100 }
        }
    ;

    CreateMultiGradient(stops, arr);

    for (new i = 0; i != sizeof arr - 1; ++i) {
        printf("%x", arr[i]);
    }

    return 1;
}
Not perfect, but hey! Feel free to upgrade it and post somewhere
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)