Skip to content

Commit

Permalink
Update main.c
Browse files Browse the repository at this point in the history
add ability to randomly select color from gradient for type-lighting

(inspired by ccMSC/ckb#264)
  • Loading branch information
pourhadi authored and frickler24 committed Feb 11, 2017
1 parent 7301ee8 commit f13e632
Showing 1 changed file with 67 additions and 23 deletions.
90 changes: 67 additions & 23 deletions src/ckb-gradient/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "../ckb/ckb-anim.h"
#include <math.h>

#include <time.h>
#include <stdio.h>
void ckb_info(){
// Plugin info
CKB_NAME("Gradient");
Expand All @@ -13,6 +14,7 @@ void ckb_info(){
// Effect parameters
CKB_PARAM_AGRADIENT("color", "Color:", "", "ffffffff");
CKB_PARAM_BOOL("kphold", "Freeze until key is released", TRUE);
CKB_PARAM_BOOL("randomize", "Randomly select from gradient", 0);

// Timing/input parameters
CKB_KPMODE(CKB_KP_NAME);
Expand Down Expand Up @@ -42,73 +44,115 @@ void ckb_info(){
#define NONE -1.f
#define HOLD -2.f

struct keyAnim {
float target;
ckb_gradient gradient;
};

ckb_gradient animcolor = { 0 };
int kphold = 0, kprelease = 0;
float* target = 0;
int kphold = 0, kprelease = 0, randomize = 0;

struct keyAnim* anim;

void ckb_init(ckb_runctx* context){
// Initialize all keys to 100% (animation over)

srand((unsigned)time(NULL));
unsigned count = context->keycount;
target = malloc(count * sizeof(float));
for(unsigned i = 0; i < count; i++)
target[i] = NONE;
anim = malloc(count * sizeof *anim);
for(unsigned i = 0; i < count; i++) {
anim[i].target = NONE;
}
}

void ckb_parameter(ckb_runctx* context, const char* name, const char* value){
CKB_PARSE_AGRADIENT("color", &animcolor){}
CKB_PARSE_BOOL("kphold", &kphold){}
CKB_PARSE_BOOL("kprelease", &kprelease){}
CKB_PARSE_BOOL("randomize", &randomize){}
}

void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){
// Start or stop animation on key
int i = key - context->keys;
if(state){
if(kphold)
target[key - context->keys] = HOLD;
else
target[key - context->keys] = 0.f;
if (randomize) {
float a, r, g, b;
float colorChoice = (float)rand()/(float)(RAND_MAX);
ckb_grad_color(&a, &r, &g, &b, &animcolor, colorChoice * 100.);
ckb_gradient newGradient = { 0 };
newGradient.ptcount = 2;
newGradient.pts[0] = 0;
newGradient.pts[1] = 100;
newGradient.a[0] = 255;
newGradient.b[0] = b;
newGradient.g[0] = g;
newGradient.r[0] = r;
newGradient.a[1] = 0;
newGradient.b[1] = b;
newGradient.g[1] = g;
newGradient.r[1] = r;

anim[i].gradient = newGradient;
}

if(kphold) {
anim[i].target = HOLD;
} else {
anim[i].target = 0.f;
}
} else {
if(kprelease)
target[key - context->keys] = NONE;
else if(kphold)
target[key - context->keys] = 0.f;
if(kprelease) {
anim[i].target = NONE;
}
else if(kphold) {
anim[i].target = 0.f;
}
}
}

void ckb_start(ckb_runctx* context, int state){
// Start/stop all keys
unsigned count = context->keycount;
if(state)
memset(target, 0, count * sizeof(float));
else {
for(unsigned i = 0; i < count; i++)
target[i] = NONE;
if(state) {
memset(anim, 0, count * sizeof *anim);
} else {
for(unsigned i = 0; i < count; i++) {
anim[i].target = NONE;
}
}
}

void ckb_time(ckb_runctx* context, double delta){
// Advance animation on each key
unsigned count = context->keycount;
for(unsigned i = 0; i < count; i++){
float phase = target[i];
float phase = anim[i].target;
if(phase > 1.f || phase < 0.f)
continue;
target[i] = phase + delta;
anim[i].target = phase + delta;
}
}

int ckb_frame(ckb_runctx* context){
// Draw key colors
unsigned count = context->keycount;
for(unsigned i = 0; i < count; i++){
float phase = target[i];
float phase = anim[i].target;

if(phase == HOLD)
phase = 0.f;
else if(phase < 0.f)
phase = 1.f;
ckb_key* key = context->keys + i;
float a, r, g, b;
ckb_grad_color(&a, &r, &g, &b, &animcolor, phase * 100.);
ckb_gradient thisGradient;
if (randomize) {
thisGradient = anim[i].gradient;
} else {
thisGradient = animcolor;
}
ckb_grad_color(&a, &r, &g, &b, &thisGradient, phase * 100.);
key->a = a;
key->r = r;
key->g = g;
Expand Down

0 comments on commit f13e632

Please sign in to comment.