diff --git a/images/Makerlab.png b/images/Makerlab.png new file mode 100644 index 0000000..241997b Binary files /dev/null and b/images/Makerlab.png differ diff --git a/src/color.rs b/src/color.rs index 61dfd9c..f29d680 100644 --- a/src/color.rs +++ b/src/color.rs @@ -60,6 +60,18 @@ where Ok(res.unwrap()) } +impl Color { + pub fn from_hex(hex: &str) -> Self { + serde_json::from_str(&format!("\"{hex}\"")).unwrap() + } +} + +impl From<&str> for Color { + fn from(value: &str) -> Self { + Color::from_hex(value) + } +} + #[cfg(test)] #[test] fn test_serialize_color() { @@ -80,7 +92,7 @@ fn test_deserialize_color() { let deserialized_color: Color = serde_json::from_str(color).unwrap(); assert_eq!(deserialized_color.r, 10); - assert_eq!(deserialized_color.g, 20); + assert_eq!(deserialized_color.g, 20); assert_eq!(deserialized_color.b, 30); assert!(serde_json::from_str::("\"000000\"").is_err()); diff --git a/src/main.rs b/src/main.rs index 7a385d3..1a1e4bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +#![feature(const_trait_impl)] +#![feature(const_convert)] + use axum::Router; use std::net::SocketAddr; diff --git a/src/polygon.rs b/src/polygon.rs index b140448..da740f3 100644 --- a/src/polygon.rs +++ b/src/polygon.rs @@ -1,17 +1,24 @@ use cairo::Context; use rand::{rngs::ThreadRng, Rng}; +use crate::color::Color; + +static COLORS: [(&str, &str); 3] = [ + ("#F4A263", "#E87052"), + ("#E0DA48", "#969A1D"), + ("#309E8F", "#2F3F52"), +]; + fn calculate_polygon_corners( center: (f64, f64), num_sides: i32, - side_length: f64, + radius: f64, rotation_angle_degrees: f64, ) -> Vec<(f64, f64)> { // no idea how this code works, it was written by ChatGPT let mut corners = Vec::new(); let rotation_angle = rotation_angle_degrees.to_radians(); let angle = std::f64::consts::PI / (num_sides as f64); - let radius = side_length / (2.0 * angle.cos()); for i in 0..num_sides { let vertex_angle = angle * (2.0 * i as f64 + 1.0 - num_sides as f64) + rotation_angle; @@ -23,41 +30,18 @@ fn calculate_polygon_corners( corners } -fn set_color_i8((r, g, b): (u8, u8, u8), context: &Context) { - context.set_source_rgb(r as f64 / 255.0, g as f64 / 255.0, b as f64 / 255.0); +fn set_color(color: Color, context: &Context) { + context.set_source_rgb( + color.r as f64 / 255.0, + color.g as f64 / 255.0, + color.b as f64 / 255.0, + ); } -fn generate_color( - rng: &mut ThreadRng, - dark_mode: bool, - unlike: Option<(u8, u8, u8)>, -) -> (u8, u8, u8) { - let criteria = |(r, g, b): (u8, u8, u8)| { - let r = r as u16; - let g = g as u16; - let b = b as u16; - if dark_mode && (r + g + b) < 300 { - return false; - } else if !dark_mode && (r + g + b) > 500 { - return false; - } - - if let Some((r1, g1, b1)) = unlike { - if (r.abs_diff(r1 as u16) + g.abs_diff(g1 as u16) + b.abs_diff(b1 as u16)) < 200 { - return false; - } - } - - return true; - }; - - let mut color = rng.gen(); - - while !criteria(color) { - color = rng.gen(); - } - - color +fn generate_colors(rng: &mut ThreadRng) -> (Color, Color) { + let color_num = rng.gen_range(0..COLORS.len()); + let (c1, c2) = COLORS[color_num]; + (Color::from_hex(c1), Color::from_hex(c2)) } pub fn draw_polygon( @@ -65,11 +49,10 @@ pub fn draw_polygon( side_length: f64, num_sides: i32, context: &Context, - dark_mode: bool, ) -> Result<(), cairo::Error> { let mut rng = rand::thread_rng(); - let c1 = generate_color(&mut rng, dark_mode, None); - set_color_i8(c1, context); + let (c1, _) = generate_colors(&mut rng); + set_color(c1, context); context.new_path(); let corners = calculate_polygon_corners(center, num_sides, side_length, 30.0); @@ -89,11 +72,10 @@ pub fn draw_segmented_polygon( side_length: f64, num_sides: i32, context: &Context, - dark_mode: bool, ) -> Result<(), cairo::Error> { let mut rng = rand::thread_rng(); - let c1 = generate_color(&mut rng, dark_mode, None); - set_color_i8(c1, context); + let (c1, c2) = generate_colors(&mut rng); + set_color(c1, context); context.new_path(); let corners = calculate_polygon_corners(center, num_sides, side_length, 30.0); @@ -112,8 +94,7 @@ pub fn draw_segmented_polygon( context.close_path(); context.fill()?; - let c2 = generate_color(&mut rng, dark_mode, Some(c1)); - set_color_i8(c2, context); + set_color(c2, context); context.new_path(); // draw other side for i in to_corner..(from_corner + 1 + 6) { @@ -129,28 +110,27 @@ pub fn draw_segmented_polygon( pub fn draw_polygon_of_segmented_polygons( center: (f64, f64), - side_length: f64, + radius: f64, + inner_radius: f64, num_sides: i32, context: &Context, - dark_mode: bool, ) -> Result<(), cairo::Error> { - let corners = calculate_polygon_corners(center, num_sides, side_length, 0.0); + let corners = calculate_polygon_corners(center, num_sides, radius, 0.0); for corner in corners { - draw_segmented_polygon(corner, side_length * 0.4, num_sides, &context, dark_mode)?; + draw_segmented_polygon(corner, inner_radius, num_sides, &context)?; } Ok(()) } pub fn draw_polygon_of_polygons( center: (f64, f64), - side_length: f64, + radius: f64, num_sides: i32, context: &Context, - dark_mode: bool, ) -> Result<(), cairo::Error> { - let corners = calculate_polygon_corners(center, num_sides, side_length, 0.0); + let corners = calculate_polygon_corners(center, num_sides, radius, 0.0); for corner in corners { - draw_polygon(corner, side_length * 0.4, num_sides, &context, dark_mode)?; + draw_polygon(corner, radius * 0.4, num_sides, &context)?; } Ok(()) } diff --git a/src/routes/favicon.rs b/src/routes/favicon.rs index a80479f..620544c 100644 --- a/src/routes/favicon.rs +++ b/src/routes/favicon.rs @@ -10,7 +10,7 @@ async fn handler() -> impl axum::response::IntoResponse { context.set_source_rgba(0.0, 0.0, 0.0, 0.0); context.fill().unwrap(); - polygon::draw_polygon_of_polygons((50.0, 50.0), 65.0, 6, &context, false).unwrap(); + polygon::draw_polygon_of_polygons((50.0, 50.0), 65.0, 6, &context).unwrap(); let mut data: Vec = Vec::new(); surface.write_to_png(&mut data).unwrap(); diff --git a/src/routes/logo.rs b/src/routes/logo.rs index 10ea96b..5ed0ab3 100644 --- a/src/routes/logo.rs +++ b/src/routes/logo.rs @@ -1,5 +1,6 @@ use axum::{extract::Query, routing::get, Router}; use cairo::{Context, Format, ImageSurface}; +use rust_embed::RustEmbed; use serde::{de, Deserialize}; use crate::{color::Color, polygon, SharedState}; @@ -29,11 +30,33 @@ struct ImageProperties { #[serde(default = "default_as_false")] #[serde(deserialize_with = "deserialize_bool")] dark_mode: bool, + #[serde(default = "default_as_false")] + #[serde(deserialize_with = "deserialize_bool")] + text: bool, background_color: Option, } +#[derive(RustEmbed)] +#[folder = "images"] +struct ImageFiles; + +fn get_surface_and_logo_coordiates(properties: &ImageProperties) -> (ImageSurface, (f64, f64)) { + if properties.text { + let image = ImageFiles::get("Makerlab.png").unwrap(); + ( + ImageSurface::create_from_png(&mut image.data.as_ref()).unwrap(), + (604.0, 432.0), + ) + } else { + ( + ImageSurface::create(Format::ARgb32, 400, 400).unwrap(), + (200.0, 200.0), + ) + } +} + async fn handler(Query(properties): Query) -> impl axum::response::IntoResponse { - let surface = ImageSurface::create(Format::ARgb32, 400, 400).unwrap(); + let (surface, logo_coordinates) = get_surface_and_logo_coordiates(&properties); let context = Context::new(&surface).unwrap(); if let Some(c) = properties.background_color { @@ -44,14 +67,8 @@ async fn handler(Query(properties): Query) -> impl axum::respon context.paint().unwrap(); - polygon::draw_polygon_of_segmented_polygons( - (200.0, 200.0), - 200.0, - 6, - &context, - properties.dark_mode, - ) - .unwrap(); + polygon::draw_polygon_of_segmented_polygons(logo_coordinates, 148.0, 67.0, 6, &context) + .unwrap(); let mut data: Vec = Vec::new(); surface.write_to_png(&mut data).unwrap(); diff --git a/src/routes/static_files.rs b/src/routes/static_files.rs index e0ef3b9..262e82e 100644 --- a/src/routes/static_files.rs +++ b/src/routes/static_files.rs @@ -9,7 +9,7 @@ use rust_embed::{EmbeddedFile, RustEmbed}; use crate::SharedState; #[derive(RustEmbed)] -#[folder = "static"] +#[folder = "web"] struct StaticFiles; async fn static_files(uri: Uri) -> impl IntoResponse { diff --git a/static/index.html b/web/index.html similarity index 79% rename from static/index.html rename to web/index.html index 753c2f4..f915332 100644 --- a/static/index.html +++ b/web/index.html @@ -26,8 +26,11 @@