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

Color work #24

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
110 changes: 94 additions & 16 deletions apps/client/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ import type { NextPage } from 'next';
import clsx from 'clsx';

import { MacTrafficLights } from '~/components/traffic-lights';
import { commands } from '~/lazy-tauri-api/get-current';

const Home: NextPage = () => {
const [added, setAdded] = useState(true);
const [color, setColor] = useState('#5F00A9');
const [color, setColor] = useState({
hex: '#5F00A9',
rgb: { r: 95, g: 0, b: 169 },
});

return (
<div className="flex h-screen w-screen flex-col rounded-lg border border-white/25 bg-black">
<div className="full h-min ps-4 pt-4" data-tauri-drag-region="true">
Expand All @@ -18,7 +23,13 @@ const Home: NextPage = () => {
<input
type="color"
className="absolute"
onChange={e => setColor(e.target.value)}
onChange={e => {
setColor(() => ({
hex: e.target.value,
rgb: hexToRgbString(e.target.value),
}));
void commands.set_color(e.target.value);
}}
/>

<div
Expand All @@ -40,7 +51,7 @@ export default Home;
const AddLight = () => {
return (
<div className="group relative aspect-square h-96 cursor-pointer select-none transition-all duration-300 hover:scale-105">
<Dots purple={true} />
<Dots purple={false} />
<div className="add-light-text absolute w-full -translate-y-[125%] text-center text-3xl font-bold text-white">
Add Light
</div>
Expand Down Expand Up @@ -117,14 +128,19 @@ const AddLight = () => {
);
};

const Dots = (props: { purple?: boolean }) => (
<div className="center-absolute transform-all pointer-events-none absolute h-[150%] w-screen">
const Dots = (props: { purple?: boolean; hue?: number }) => (
<div
className="center-absolute transform-all pointer-events-none absolute h-[150%] w-screen"
style={{
filter: `hue-rotate(${props.hue ?? 0}deg)`,
}}
>
<div
className={clsx(
'absolute h-full w-[200%] -translate-x-1/4 bg-repeat transition-all duration-300 group-hover:scale-[.85]',
{
dots: props.purple,
'dots-purple': !props.purple,
dots: !props.purple,
'dots-purple': props.purple,
},
)}
></div>
Expand All @@ -145,13 +161,25 @@ const Dots = (props: { purple?: boolean }) => (
</div>
);

const Light = (props: { name: string; color: string }) => {
const Light = (props: {
name: string;
color: {
hex: string;
rgb: {
r: number;
g: number;
b: number;
};
};
}) => {
return (
<div className="group relative aspect-square h-96 cursor-pointer select-none transition-all duration-300 hover:scale-105">
<div className="absolute bottom-0 z-10 translate-y-[125%] text-xl text-white/70">
{props.name}
</div>
<Dots />
<Dots
hue={props.color.rgb.r * props.color.rgb.g * props.color.rgb.b}
/>

<div
className="add-light-bg-gradient absolute h-full w-full overflow-hidden rounded-md"
Expand All @@ -167,9 +195,24 @@ const Light = (props: { name: string; color: string }) => {
style={{
background:
'conic-gradient(from 90deg at 50% 50%,' +
'rgba(82, 0, 255, 0.00) 110.62499642372131deg,' +
'rgba(143, 0, 255, 0.38) 206.25000715255737deg,' +
'rgba(77, 0, 203, 0.30) 333.7499928474426deg)',
`rgba(${combineColor(
props.color.rgb.r,
48,
)}, 0, ${combineColor(
props.color.rgb.b,
86,
)}, 0.00) 110.62499642372131deg,` +
`rgba(${props.color.rgb.r + 143}, 0, ${combineColor(
props.color.rgb.b,
86,
)}, 0.38) 206.25000715255737deg,` +
`rgba(${combineColor(
props.color.rgb.r,
-18,
)} , 0, ${combineColor(
props.color.rgb.b,
34,
)}, 0.30) 333.7499928474426deg)`,
}}
></div>
<img
Expand All @@ -181,17 +224,32 @@ const Light = (props: { name: string; color: string }) => {
<div
className="absolute h-full w-full rounded-md opacity-95"
style={{
background: props.color,
background: props.color.hex,
}}
></div>
<div
className="absolute h-full w-full rounded-md blur-md"
style={{
background:
'conic-gradient(from 180deg at 50% 50%,' +
'rgba(82, 0, 255, 0.00) 110.62499642372131deg,' +
'#8F00FF 206.25000715255737deg,' +
'rgba(77, 0, 203, 0.79) 333.7499928474426deg)',
`rgba(${combineColor(
props.color.rgb.r,
48,
)}, 0, ${combineColor(
props.color.rgb.b,
86,
)}, 0.00) 110.62499642372131deg,` +
`rgba(${props.color.rgb.r + 143}, 0, ${combineColor(
props.color.rgb.b,
86,
)}, 0.38) 206.25000715255737deg,` +
`rgba(${combineColor(
props.color.rgb.r,
-18,
)} , 0, ${combineColor(
props.color.rgb.b,
34,
)}, 0.76) 333.7499928474426deg)`,
}}
></div>
<img
Expand All @@ -218,3 +276,23 @@ const Light = (props: { name: string; color: string }) => {
</div>
);
};

const combineColor = (color: number, color2: number) => {
const sum = color + color2;
if (sum > 255) {
return 255;
} else if (sum < 0) {
return 0;
} else {
return sum;
}
};

const hexToRgbString = (hex: string) => {
const bigint = parseInt(hex.replace('#', ''), 16);
const r = (bigint >> 16) & 255;
const g = (bigint >> 8) & 255;
const b = bigint & 255;

return { r, g, b };
};
7 changes: 7 additions & 0 deletions apps/client/src/lazy-tauri-api/get-current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ export const window = {
return await importGetCurrent();
},
};

export const commands = {
async set_color(color: string) {
const { invoke } = await import('@tauri-apps/api/tauri');
return invoke<void>('set_color', { color });
},
};
49 changes: 49 additions & 0 deletions apps/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ tauri-build = { version = "1.3", features = [] }
tauri = { version = "1.3", features = ["macos-private-api", "shell-open", "window-close", "window-minimize", "window-set-fullscreen", "window-start-dragging"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1", features = ["full"] }

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
54 changes: 50 additions & 4 deletions apps/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,61 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

use std::sync::Arc;

use tokio::io::AsyncWriteExt;

use tokio::net::{TcpSocket, TcpStream};
use tokio::sync::{Mutex, MutexGuard};

pub struct MutexState(Mutex<InnerState>);
struct InnerState {
pub b: u8,
pub tcp_stream: Option<TcpStream>,
// pub tpc_socket: ,
}

// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
async fn set_color(color: &str, state: tauri::State<'_, MutexState>) -> Result<(), ()> {
println!("color: {}", color);
if let Some(inner) = state.0.lock().await.tcp_stream.as_mut() {
match inner.write_all(format!("{}00", color).as_bytes()).await {
Ok(_) => println!("ok"),
Err(e) => println!("error: {:?}", e),
}
}
// state
// .tcp_stream
// .as_mut()
// .unwrap()
// .write_all(b"#00110011")
// .await
// .unwrap();
// state.b += 1;
// println!("b: {} {}", state.b, color);
Ok(())
}

fn main() {
#[tokio::main]
async fn main() {
let mut stream = TcpStream::connect("10.0.0.12:1234").await;

let tcp_stream = match stream {
Ok(stream) => Some(stream),
Err(e) => {
println!("failed to connect to server: {:?}", e);
None
}
};
// Write some data.

// let socket = Some(socket);

// let stream = socket.connect(addr).await?;
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.manage(MutexState(Mutex::new(InnerState { b: 0, tcp_stream })))
.invoke_handler(tauri::generate_handler![set_color])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}