ColorBetween function. Fail.
#1

1.
Код:
stock ColorBetween(ColorA, ColorB, Float:Percent)
{
    new R1, G1, B1, A1, R2, G2, B2, A2;

    HexToInt(ColorA, R1, G1, B1, A1);
    HexToInt(ColorB, R2, G2, B2, A2);

    return IntToHex(
        floatround(Percent * (R2-R1)) + R1,
        floatround(Percent * (G2-G1)) + G1,
        floatround(Percent * (B2-B1)) + B1,
        floatround(Percent * (A2-A1)) + A1
    );
}
2.
Код:
stock HexToInt(hexcolor, &r, &g, &b, &a)
{
	r = ( hexcolor >> 32 ) & 0xFF;
	g = ( hexcolor >> 16 ) & 0xFF;
	b = ( hexcolor >> 8 ) & 0xFF;
	a = hexcolor & 0xFF;
}
stock IntToHex(r, g, b, a = 255, &hexcolor = -1) 
{
	hexcolor = (r*16777216)+(g*65536)+(b*256)+a;
	return hexcolor;
}
3. The hex-int functions works perfect. The problem is in the ColorBetween function.

4. Doing ColorBetween(0xFF7F3F1F, 0x00000000, 0.5) returns 0x10402010. Obviously this is wrong.






Does anyone know how to do this right? It's 3 in the morning here, I'm tired. It's probably something small and easy that I'm not noticing. Thanks.
Reply
#2

Just make sth like this:

Код:
stock ColorBetween(ColorA, ColorB) {
    new str[11];
    valstr(str,ColorA);
    new x = strval(str);
    valstr(str,ColorB);
    new y = strval(str);
    return x-y;
}

printf("0x%x",ColorBetween(0xF, 0xA));
//-> Gives u 0x5
But attention ColorA must be >= ColorB
Reply
#3

At the very least, the right shift in your HexToInt function for red is wrong. This should be 24. Also you should use the logical right shift (>>>, three shevrons) instead of the arithmetic right shift (>>). With the arithmetic right shift the number will be padded with the value of the MSB, so if you try to shift anything with a red value greater than 7F the number will be padded with 1 in the front and you will end up with very weird results.
Reply
#4

Quote:
Originally Posted by Kaliber
Посмотреть сообщение
Just make sth like this:

Код:
stock ColorBetween(ColorA, ColorB) {
    new str[11];
    valstr(str,ColorA);
    new x = strval(str);
    valstr(str,ColorB);
    new y = strval(str);
    return x-y;
}

printf("0x%x",ColorBetween(0xF, 0xA));
//-> Gives u 0x5
But attention ColorA must be >= ColorB
Not an option at the moment.
Quote:
Originally Posted by Vince
Посмотреть сообщение
At the very least, the right shift in your HexToInt function for red is wrong. This should be 24. Also you should use the logical right shift (>>>, three shevrons) instead of the arithmetic right shift (>>). With the arithmetic right shift the number will be padded with the value of the MSB, so if you try to shift anything with a red value greater than 7F the number will be padded with 1 in the front and you will end up with very weird results.
I always have sucked with shifts. Could you show me what you mean?
Reply
#5

Quote:
Originally Posted by Crayder
Посмотреть сообщение
I always have sucked with shifts. Could you show me what you mean?
So first of all write it like this:

Код:
stock HexToInt(hexcolor, &r, &g, &b, &a)
{
    r = ( hexcolor >> 24) & 0xFF;
    g = ( hexcolor >> 16 ) & 0xFF;
    b = ( hexcolor >> 8 ) & 0xFF;
    a = hexcolor & 0xFF;
}
If that doesn't work...write it like this (Thats the other shift):

Код:
stock HexToInt(hexcolor, &r, &g, &b, &a)
{
    r = ( hexcolor >>> 24) & 0xFF;
    g = ( hexcolor >>> 16 ) & 0xFF;
    b = ( hexcolor >>> 8 ) & 0xFF;
    a = hexcolor & 0xFF;
}
Greekz
Reply
#6

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Not an option at the moment.
I always have sucked with shifts. Could you show me what you mean?
I think this page explains it rather well: https://sampwiki.blast.hk/wiki/Binary

Taking your example from earlier:
Код:
FF7F3F1F // hex
11111111 01111111 00111111 00011111
^ Most significant bit (MSB)

Arithmetic right shift by 24:
11111111 11111111 11111111 11111111
^^^^^^^^ ^^^^^^^^ ^^^^^^^^
padding is the value of the MSB (1)

Logical right shift by 24:
00000000 00000000 00000000 11111111
^^^^^^^^ ^^^^^^^^ ^^^^^^^^
padding is just 0
Edit: I didn't pay attention to the "& 0xFF". In any case that should only select the lower 8 bits, no matter what kind of shift is used.
Reply
#7

Quote:
Originally Posted by Vince
Посмотреть сообщение
Edit: I didn't pay attention to the "& 0xFF". In any case that should only select the lower 8 bits, no matter what kind of shift is used.
So I had it right (except the '>>> 24' part)? The conversion functions seem to be working fine. But the ColorBetween is still not working.

Edit: Wait, it might be working now... Brb... So far I'm pretty sure it is...






EDIT: Now that I have that... The following only returns an array with the first gradient (between the first two parameters, but it does loop though all given).
pawn Код:
stock GenerateGradient(...)
{
    new Colors[263], Steps = (263/numargs()), Color[263];
    for(new i; i<numargs()-1; i++)
    {
        ColorsBetween(getarg(i), getarg(i+1), Steps, Color);
        for(new j; j<Steps; j++) Colors[(i*Steps)+j] = Color[j];
    }
    return Colors;
}
Help please!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)