Answer the question
In order to leave comments, you need to log in
How to smoothly change an integer over some time from one value to another?
There is a color that is specified as an integer value in the extended HEX format with an alpha channel (HEXA).
For example, 0x12ABCDFF. Here alpha channel = 0xFF, i.e. without transparency.
You need to write a function with the following prototype:
void fadeColor(int &hexa, int duration, bool in = false);
The essence of its work:
The color itself is accepted (or rather, the address of the variable where it is stored), the transparency of which (alpha channel) should change during the duration time (in ms) or from the current value (taken from hexa) to 0xFF (then the argument in == true , which means fade in - smooth appearance), or from the current value to 0x00 (argument in == false, which means fade out - smooth disappearance)
Here is the pseudocode of what I wrote:
void fadeColor(int &hexa, int duration, bool in = false)
{
int from = hexa; // начальное значение цвета
int to = (in) ? (from | 0xFF) : (from - (from & 0xFF)); // конеченое значение цвета
// у конечного значения альфа меняется либо на 00 либо на FF в зависимости от in.
int update_rate = (to - from)/duration; // шаг изменения цвета за 1 мс
SetTimer("_onColorFade", 1, "ddd", from, to, update_rate); // внизу объясню, что это за функция
}
void _onColorFade(int &from, int to, int update_rate)
{
from += update_rate; // меняем цвет
if (from == to) // если дошли до конечного цвета, то останавливаемся
return;
// иначе продолжаем менять цвет
SetTimer("_onColorFade", 1, "ddd", from, to, update_rate); // снова запускаем
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question