void ckb_start(ckb_runctx* context, int state){ if(state == 0){ // Stop animation phase = -1.; return; } // Start animation phase = 0.; ckb_key* keys = context->keys; unsigned count = context->keycount; // Randomize starting colors and pick a random target newtarget(current, count); // Over the course of the animation, the keys fade between the current pattern and the target pattern newtarget(target, count); // Set all keys to current for(unsigned i = 0; i < count; i++){ ckb_key* key = keys + i; if(fadein) current[i].a = 0; key->a = current[i].a; key->r = current[i].r; key->g = current[i].g; key->b = current[i].b; } }
void ckb_start(ckb_runctx* context){ phase = 0.; ckb_key* keys = context->keys; unsigned count = context->keycount; newtarget(current, count); newtarget(target, count); // Set all keys to current for(unsigned i = 0; i < count; i++){ ckb_key* key = keys + i; if(fadein) current[i].a = 0; key->a = current[i].a; key->r = current[i].r; key->g = current[i].g; key->b = current[i].b; } }
void ckb_time(ckb_runctx* context, double delta){ if(phase < 0.) return; // Advance animation phase += delta; if(phase > 1.){ // If the animation is complete, pick a new target pattern and start again phase -= 1.; rgb* temp = target; target = current; current = temp; newtarget(target, context->keycount); } }
int ckb_frame(ckb_runctx* context, double delta){ if(phase < 0.) // Generate an initial state if it hasn't been done yet ckb_start(context); ckb_key* keys = context->keys; unsigned count = context->keycount; phase += delta; if(phase >= 1.){ phase -= 1.; rgb* temp = target; target = current; current = temp; newtarget(target, count); } for(unsigned i = 0; i < count; i++){ ckb_key* key = keys + i; key->a = round(current[i].a * (1. - phase) + target[i].a * phase); key->r = round(current[i].r * (1. - phase) + target[i].r * phase); key->g = round(current[i].g * (1. - phase) + target[i].g * phase); key->b = round(current[i].b * (1. - phase) + target[i].b * phase); } return 0; }