0

With the following code, I'm supposed to be able to do WC_InterpolateColor(0xFF0000, 0xFFFF00, 0.5) and get the color that is halfway in between the two colors specified (red and yellow in this case). The pos value should be between 1.0 and 0.0. The code is obviously unfinished and I don't know where to go next with it. Has anybody else tried doing something like this? Can somebody show me how to do it correctly? Right now the code always returns the end color (yellow in this case). Thanks ahead of time, and please explain so I can learn from it.

WC_InterpolateColor(start, end, Float:pos) {
    new start_bytes[1], end_bytes[1];
    start_bytes[0] = start;
    end_bytes[0] = end;

    for (new i = 0; i < 4; i++) {
        start_bytes{i} = floatround(start_bytes{i} * (1.0-pos) + end_bytes{i} * pos);
    }

    return end_bytes[0];
}

Ps. The language used is Pawn, a language very similar to C++. The keyword new is like doing int or float, in this case int.

IS4
  • 11,945
  • 2
  • 47
  • 86
Corey Iles
  • 155
  • 1
  • 17

1 Answers1

0

In fact, you are exteremely close to your goal: just replace start_bytes{i} = with end_bytes{i} =.

However, I was also trying to make a similar function, and I have taken in account that the color byte is actually the square root of the color component intensity, so I first square it and then blend it. Here's an excerpt from i_colors:

stock BlendColors(col1, col2, Float:t)
{
    new c1[4 char], c2[4 char];
    GetColorComponentsArray(col1, c1);
    GetColorComponentsArray(col2, c2);
    if(c2{3} == 0)
    {
        return CreateColorRGBA(
            c1{0}, c1{1}, c1{2},
            BlendAlphaComponent(c1{3}, c2{3}, t)
        );
    }else{
        return CreateColorRGBA(
            BlendColorComponent(c1{0}, c2{0}, t),
            BlendColorComponent(c1{1}, c2{1}, t),
            BlendColorComponent(c1{2}, c2{2}, t),
            BlendAlphaComponent(c1{3}, c2{3}, t)
        );
    }
}

static stock GetColorComponentsArray(color, components[4 char])
{
    components[0] = color;
}

static stock CreateColorRGBA(r, g, b, a=0xFF)
{
    return (r << 24) | (g << 16) | (b << 8) | a;
}

static stock BlendColorComponent(c1, c2, Float:t)
{
    return floatround(floatsqroot((1.0-t)*c1*c1 + t*c2*c2));
}

static stock BlendAlphaComponent(a1, a2, Float:t)
{
    return floatround((1.0-t)*a1 + t*a2);
}

We got the same idea to store the color in one-cell array and then access the individual bytes, but 4 char is compatible with machines that have a smaller cell size (and is equivalent to 1 in case of 4-byte cells).

IS4
  • 11,945
  • 2
  • 47
  • 86