This project is a palette creator tool that allows users to generate and customize color palettes for their design projects.
1// Utility buttons for testing palette, resetting colors, etc.
2
3<template>
4 <!-- wrapper -->
5 <div class="utility-buttons" data-testid="utility-buttons">
6 <!-- individual buttons -->
7
8 <button
9 class="main-button"
10 @click="oneShotPalette"
11 data-testid="one-shot-button"
12 data-tooltip="Random color + random scheme + apply to UI in one click"
13 >
14 <i class="fas fa-dice"></i>
15 One Shot
16 </button>
17
18 <button
19 class="secondary-button"
20 @click="setRandomScheme"
21 data-testid="random-scheme-button"
22 data-tooltip="Randomize all 5 slots"
23 >
24 <i class="fas fa-random" title="Generate random scheme" />
25 Random Variations
26 </button>
27
28 <button
29 v-if="showButtons"
30 class="secondary-button"
31 data-testid="test-palette-button"
32 data-tooltip="Apply this palette to the UI so you can preview it live"
33 @click="setCssVars"
34 >
35 <i class="fas fa-vial"></i>
36 Test this palette
37 </button>
38
39 <button
40 v-if="showButtons"
41 :class="[
42 'secondary-button',
43 { 'reset-highlight': store.state.isTestingColorScheme },
44 ]"
45 @click="resetSiteColors"
46 data-testid="reset-site-colors-button"
47 data-tooltip="Restore the UI to its default colors"
48 >
49 <i class="far fa-window-restore"></i>
50 Reset site colors
51 </button>
52
53 <button
54 v-if="showButtons"
55 class="secondary-button"
56 data-testid="set-light-text-button"
57 data-tooltip="Set text color to light"
58 @click="setLightText"
59 >
60 <i class="far fa-lightbulb"></i>
61 Light Text
62 </button>
63
64 <button
65 v-if="showButtons"
66 class="secondary-button"
67 data-testid="set-dark-text-button"
68 data-tooltip="Set text color to dark"
69 @click="setDarkText"
70 >
71 <i class="fas fa-lightbulb"></i>
72 Dark Text
73 </button>
74
75 <button
76 v-if="showButtons"
77 class="secondary-button"
78 data-testid="export-css-button"
79 data-tooltip="Copy the palette as CSS variables to your clipboard"
80 @click="copyPalette"
81 >
82 <i class="far fa-copy"></i>
83 Export CSS
84 </button>
85
86 <button
87 v-if="showButtons"
88 class="secondary-button"
89 data-testid="save-palette-button"
90 data-tooltip="Save this palette to local storage"
91 @click="savePalette"
92 >
93 <i class="fas fa-save"></i>
94 Save Palette
95 </button>
96 </div>
97</template>
98
99<script setup>
100 import { computed } from 'vue';
101 import { useStore } from 'vuex';
102
103 import { DEFAULT_HEX_COLORS, DEFAULT_LIGHT_COLORS } from '../lib/colors';
104
105 const emit = defineEmits(['copyPalette', 'savePalette']);
106
107 const store = useStore();
108
109 const showButtons = computed(() => store.getters.fullSchemeSet);
110
111 /** Sets text color to the default light color for the current theme */
112 const setLightText = () => {
113 if (store.state.isTestingColorScheme) {
114 const { hex, hsl, rgb } = store.state.slotColors.slot4;
115 document.documentElement.style.setProperty('--text-color', hex);
116 store.commit('SET_TEXT_COLOR', { hex, hsl, rgb });
117 } else {
118 const hex =
119 store.state.theme === 'light'
120 ? DEFAULT_LIGHT_COLORS.LIGHT
121 : DEFAULT_HEX_COLORS.LIGHT;
122 document.documentElement.style.setProperty('--text-color', hex);
123 store.dispatch('SET_TEXT_COLOR', 'light');
124 }
125 };
126
127 /** Sets text color to the default dark color for the current theme */
128 const setDarkText = () => {
129 if (store.state.isTestingColorScheme) {
130 const { hex, hsl, rgb } = store.state.slotColors.slot5;
131 document.documentElement.style.setProperty('--text-color', hex);
132 store.commit('SET_TEXT_COLOR', { hex, hsl, rgb });
133 } else {
134 const hex =
135 store.state.theme === 'light'
136 ? DEFAULT_LIGHT_COLORS.DARK
137 : DEFAULT_HEX_COLORS.DARK;
138 document.documentElement.style.setProperty('--text-color', hex);
139 store.dispatch('SET_TEXT_COLOR', 'dark');
140 }
141 };
142
143 /** Copies the current palette to the clipboard */
144 const copyPalette = () => {
145 emit('copyPalette');
146 };
147
148 /** Saves the current palette */
149 const savePalette = () => {
150 emit('savePalette');
151 };
152
153 /** Picks a unique random color from the variations and sets it on the main palette pane */
154 const setRandomScheme = () => {
155 store.dispatch('SET_RANDOM_SCHEME');
156 };
157
158 /** Generates a random main color, randomizes all slots, and applies the palette to the UI in one action */
159 const oneShotPalette = () => {
160 store.dispatch('SET_MAIN_COLOR');
161 store.dispatch('SET_RANDOM_SCHEME');
162 setCssVars();
163 };
164
165 /** Resets the site colors to default by removing inline overrides */
166 const resetSiteColors = () => {
167 for (const v of [
168 '--clr-main',
169 '--clr-secondary',
170 '--clr-accent',
171 '--clr-light',
172 '--clr-dark',
173 '--text-color',
174 ]) {
175 document.documentElement.style.removeProperty(v);
176 }
177 store.commit('SET_IS_TESTING', false);
178 const textType = store.state.theme === 'dark' ? 'light' : 'dark';
179 store.dispatch('SET_TEXT_COLOR', textType);
180 };
181
182 /** Sets the CSS variables to the current palette colors, allowing the user to test the palette on the site */
183 const setCssVars = () => {
184 const main = store.state.mainSlotColor.hex;
185 const secondary = store.state.slotColors.slot2.hex;
186 const accent = store.state.slotColors.slot3.hex;
187 const light = store.state.slotColors.slot4.hex;
188 const dark = store.state.slotColors.slot5.hex;
189
190 document.documentElement.style.setProperty('--clr-main', main);
191 document.documentElement.style.setProperty(
192 '--clr-secondary',
193 secondary,
194 );
195 document.documentElement.style.setProperty('--clr-accent', accent);
196 document.documentElement.style.setProperty('--clr-light', light);
197 document.documentElement.style.setProperty('--clr-dark', dark);
198
199 const textSlot =
200 store.state.theme === 'dark'
201 ? store.state.slotColors.slot4
202 : store.state.slotColors.slot5;
203 document.documentElement.style.setProperty(
204 '--text-color',
205 textSlot.hex,
206 );
207 store.commit('SET_TEXT_COLOR', {
208 hex: textSlot.hex,
209 hsl: textSlot.hsl,
210 rgb: textSlot.rgb,
211 });
212
213 store.commit('SET_IS_TESTING', true);
214 };
215</script>
216
217<style scoped>
218 .reset-highlight {
219 border-color: var(--clr-accent, #7c6af7);
220 border-width: 2px;
221 }
222</style>