aboutsummaryrefslogtreecommitdiff
path: root/src/effects/rainbow.c
blob: 06b671c8a6e6487d0386fe8302ec23b6669bf637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

#include "scheme.h"

#include "../any_log.h"

typedef struct {
    effect_t effect;
    unsigned int speed;
    cairo_pattern_t *gradient;
} effect_rainbow_t;

extern double cubic_bezier(double x, double a, double b, double c, double d);

static void effect_rainbow_pre(effect_t *effect, layout_t *layout, cairo_t *cr)
{
    struct timespec now;
    timespec_get(&now, TIME_UTC);

    struct timespec diff = timespec_diff(now, effect->start);
    double t = (double)timespec_to_ms(diff) / timespec_to_ms(effect->info->duration);
    double s = cubic_bezier(t, 0.19, 1.0, 0.22, 1.0);
    (void ) s;;

    effect_rainbow_t *rainbow = (effect_rainbow_t *)effect;
    double degree = M_PI / 180.0;
    int radius = layout->height / 2 - layout->y_padding;

    cairo_matrix_t matrix;
    cairo_matrix_init_translate(&matrix, s, 0);
    //cairo_matrix_scale(&matrix, layout->x, layout->y);
    cairo_pattern_set_matrix(rainbow->gradient, &matrix);
    cairo_set_source(cr, rainbow->gradient);

    cairo_new_sub_path(cr);
    cairo_arc(cr, layout->x + layout->x_padding + radius, layout->y + layout->y_padding + radius, radius, 90 * degree, 270 * degree);
    cairo_arc(cr, layout->x + layout->width - layout->x_padding - radius, layout->y + layout->y_padding + radius, radius, 270 * degree, 450 * degree);
    cairo_close_path(cr);
    cairo_fill(cr);
}

static effect_t *effect_rainbow_allocate(const effect_info_t *info)
{
    effect_rainbow_t *effect = malloc(sizeof(effect_rainbow_t));
    effect_init((effect_t *)effect, info);

    effect->speed = 1;
    effect->gradient = cairo_pattern_create_linear(0, 0, 1, 0);
    cairo_pattern_set_extend(effect->gradient, CAIRO_EXTEND_REPEAT);

    unsigned int colors[] = {
        0xff0000, 0xff7300, 0xfffb00,
        0x48ff00, 0x00ffd5, 0x002bff,
        0x7a00ff, 0xff00c8, 0xff0000,
    };

    for (int i = 0; i < 9; i++) {
        color_t color = color_hex(colors[i]);
        cairo_pattern_add_color_stop_rgb(effect->gradient, i * (1.0 / 9.0),
                                         color.r, color.g, color.b);
    }

    return (effect_t *)effect;
}

static void effect_rainbow_finalize(effect_rainbow_t *effect)
{
    cairo_pattern_destroy(effect->gradient);
    free(effect);
}

static bool effect_rainbow_validate(effect_info_t *info, const effect_scheme_t *scheme)
{
    return true;
}

static const config_entry_t effect_rainbow_entries[] = {
    { 0 },
};

const effect_scheme_t effect_rainbow_scheme = {
    .name = "rainbow",
    .info = {
        .duration = {
            .tv_sec = 0,
            .tv_nsec = 200000000,
        },
        .allocate = effect_rainbow_allocate,
        .finalize = (effect_finalize_t)effect_rainbow_finalize,
        .pre = effect_rainbow_pre,
    },
    .size = sizeof(unsigned int),
    .entries = effect_rainbow_entries,
    .validate = effect_rainbow_validate,
};