aboutsummaryrefslogtreecommitdiff
path: root/src_frontend/Components/MainControls/ControlColors.svelte
blob: 5d21cc02c9a84d236e094db551ce0b04829d5a0c (plain) (blame)
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
100
101
102
103
104
105
106
107
<script>
    import { writable } from 'svelte/store';
    import { createEventDispatcher } from 'svelte';
    import { fade } from 'svelte/transition';

    import PrettyVar from "../../ComponentLib/PrettyVar.svelte";
    import iro from '@jaames/iro';
    
    const dispatch = createEventDispatcher();

    let wrapperWidth = writable(0);

    // This is a list of color variables that we can change
    export let colorVariables = {
        "Main": "#1bcf3f",
        "Second": "#fafafa",
        "tert": "#fafafa"
    };
    // These are some helper functions for the object
    const getColorOfVar = function(name) { return colorVariables[name]; }
    const setColorOfVar = function(name, color) { colorVariables[name] = color; }
    // This is the identifier of the variable that currently is selected for changing
    let currentVariable = Object.keys(colorVariables)[0];

    let colorPicker;
    function picker(node) {
        colorPicker = new iro.ColorPicker(node, {
            color: getColorOfVar(currentVariable),
            width: $wrapperWidth
        });
        colorPicker.on('color:change', function(color) {
            setColorOfVar(currentVariable, color.hexString);
            dispatch("change", {name: currentVariable, value: color.hexString});
        });

        wrapperWidth.subscribe((width) => {
            colorPicker.resize(width - 30);
        });
        
        return {
			destroy() {}
		};
    }

    function changeColorVar(ev) {
        currentVariable = ev.target.dataset.id;
        colorPicker.color.hexString = getColorOfVar(currentVariable);
    }

</script>

<style>
    .wrapper {
        width: 100%;
        box-sizing: border-box;
        border-radius: 15px;
    }
    
    .color-picker {
        padding: 15px;
    }

    .color-options {
        margin-top: 15px;
        display: flex;
        width: 100%;
        justify-content: center;
        align-items: center;
    }
    .color-options > * {
        margin: auto;
        width: 100%;
        text-align: center;
        padding: 15px;
        box-sizing: border-box;
        border-top: 2px solid;
    }
    .color-options > *:first-child {
        border-bottom-left-radius: 15px;
    }
    .color-options > *:last-child {
        border-bottom-right-radius: 15px;
    }
    .color-options .selected {
        background-color: #eaeaea;
    }
    .color-options > *:hover {
        background-color: #dcdcdc;
    }
    .color-options > *:active {
        background-color: #d4d4d4;
    }
</style>

<div transition:fade|local class="wrapper drop-shadow" bind:clientWidth={$wrapperWidth} >
    <div class="color-picker" use:picker></div>
    <div class="color-options">
        {#each Object.keys(colorVariables) as colorVar}
            <span style="border-top-color: {getColorOfVar(colorVar)}" 
                  data-id={colorVar}
                  on:click={changeColorVar}
                  class:selected={colorVar == currentVariable}>
                <PrettyVar varText={colorVar} />
            </span>
        {/each}
    </div>
</div>