Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ColorSetting): chroma effect options #4800

Draft
wants to merge 6 commits into
base: nextgen
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src-theme/src/integration/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export interface ColorSetting {
valueType: string;
name: string;
value: number;
rainbowMode: string;
rainbowSpeed: number;
}

export interface BooleanSetting {
Expand Down
146 changes: 0 additions & 146 deletions src-theme/src/routes/clickgui/setting/ColorSetting.svelte

This file was deleted.

197 changes: 197 additions & 0 deletions src-theme/src/routes/clickgui/setting/color/ColorSetting.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
<script lang="ts">
import "@simonwep/pickr/dist/themes/classic.min.css";
import "./pickr.scss";
import {createEventDispatcher, onMount} from "svelte";
import type {ColorSetting, ModuleSetting} from "../../../../integration/types.js";
import Pickr from "@simonwep/pickr";
import {convertToSpacedString, spaceSeperatedNames} from "../../../../theme/theme_config";
import {intToRgba, rgbaToHex, rgbaToInt} from "../../../../integration/util";
import RainbowSpeed from "./Rainbow.svelte";
import {Color4b} from "./color_utils"

export let setting: ModuleSetting;

const cSetting = setting as ColorSetting;

const dispatch = createEventDispatcher();

let colorPicker: HTMLElement;
let pickr: Pickr;
let hidden = true;

let hex = rgbaToHex(intToRgba(cSetting.value));
let color = "rgba(255,255,255,0)"
let interval;

onMount(() => {
pickr = Pickr.create({
el: colorPicker,
theme: "classic",
showAlways: true,
inline: true,
default: rgbaToHex(intToRgba(cSetting.value)),

components: {
preview: false,
opacity: true,
hue: true,

interaction: {
hex: false,
rgba: false,
hsla: false,
hsva: false,
cmyk: false,
input: false,
clear: false,
save: false,
},
},
});

pickr.on("change", (v: any) => {
hex = v.toHEXA().toString();

const [r, g, b, a] = v.toRGBA();
const rgba = [r, g, b, a * 255];

cSetting.value = rgbaToInt(rgba);
setting = {...cSetting};
dispatch("change");
});

interval = setInterval(() => {
if (cSetting) {
setColor();
}
}, 16);
});

function handleValueInput() {
pickr.setColor(hex);
setColor()
}

function calculateColorCycle(): string {
const currentColor = Color4b.fromPacked(cSetting.value);
const time = Date.now();
const frequency = getFrequency();
currentColor.hue((time % frequency) / frequency);
return currentColor.asHex()
}

function calculateColorPulse(): string {
const currentColor = Color4b.fromPacked(cSetting.value);
const time = Date.now();
const frequency = getFrequency();
const oscillation = Math.abs((time % (frequency * 2)) - frequency) / frequency;
const brightness = 0.3 - 0.3 * oscillation + Math.min(currentColor.getBrightness(), 0.7);
currentColor.brightness(brightness);
return currentColor.asHex()
}

function getFrequency(): number {
return (200 + 1 - cSetting.rainbowSpeed) * 200;
}

function setColor() {
switch (cSetting.rainbowMode) {
case "None": {
color = hex;
return
}
case "Cycle": {
color = calculateColorCycle();
return;
}
case "Pulse": {
color = calculateColorPulse();
return;
}
}

color = "rgba(255,255,255,0)";
}
</script>

<div class="setting">
<div class="name">{$spaceSeperatedNames ? convertToSpacedString(cSetting.name) : cSetting.name}</div>
<div class="value-spot">
<input
class="value"
bind:value={hex}
on:input={handleValueInput}
/>
<button
class="color-pickr-button"
on:click={() => (hidden = !hidden)}
style="background-color: {color};"
></button>
</div>
<div class="color-picker" class:hidden>
<button bind:this={colorPicker}/>
<RainbowSpeed bind:setting={setting} on:change={() => dispatch("change")}/>
</div>
</div>

<style lang="scss">
@import "../../../../colors.scss";

.setting {
display: grid;
grid-template-areas:
"a b"
"c c";
padding: 7px 0px;
}

.name {
grid-area: a;
font-weight: 500;
color: $clickgui-text-color;
font-size: 12px;
}

.hidden {
height: 0px;
display: none;
}

.value {
font-weight: 500;
color: $clickgui-text-color;
text-align: right;
font-size: 12px;
cursor: text;
text-transform: uppercase;
background-color: transparent;
border: none;
padding: 0;
margin: 0 15px 0 auto;
width: 70px;
font-family: monospace;
}

.value-spot {
grid-area: b;
display: flex;
align-items: stretch;
}

.color-picker {
grid-area: c;
}

.color-pickr-button {
margin-top: -2px;
margin-bottom: -2px;
width: 30px;
border-radius: 3px;
background-color: blue;
border-style: none;
}

.color-pickr-button:focus {
outline: 3px solid #ffffff;
}
</style>
Loading
Loading