Skip to content

Commit

Permalink
added a UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Datron committed Aug 4, 2024
1 parent 34bcf07 commit 6cea859
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 52 deletions.
23 changes: 19 additions & 4 deletions Cargo.lock

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

13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
worker = "0.3.0"
worker = { version = "0.3.0", features = ['http', 'axum'] }
image = "0.25.1"
imageproc = "0.25.0"
ab_glyph = "=0.2.26"
rand = "0.8.5"
tracing = "0.1"
tracing-web = "0.1"
tracing-subscriber = { version = "0.3", features=['time', 'json'] }
time = { version = "0.3", features=['wasm-bindgen'] }
tracing-subscriber = { version = "0.3", features = ['time', 'json'] }
time = { version = "0.3", features = ['wasm-bindgen'] }
serde = "1.0.204"
serde_json = "1.0.117"
axum = { version = "0.7", default-features = false }
axum = { version = "0.7", default-features = false, features = ["json"]}
tower-service = "0.3.2"


[profile.release]
opt-level = "s" # optimize for size in release builds
opt-level = "s" # optimize for size in release builds
lto = true
strip = true
codegen-units = 1
109 changes: 76 additions & 33 deletions src/image_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,75 @@ use ab_glyph::{Font, FontRef, Rect};
use image::{ImageBuffer, Rgb, RgbImage};
use imageproc::drawing::{draw_filled_circle_mut, draw_filled_rect_mut};
use rand::{rngs::ThreadRng, Rng};
use serde::Deserialize;

#[derive(Debug, Clone)]
struct ColourPallete {
pub(crate) struct ColourPallete {
pub number_colours: Vec<Rgb<u8>>,
pub background_colours: Vec<Rgb<u8>>,
}

#[derive(Debug)]
enum ColourBlindnessType {
RedGreen,
// Green
#[derive(Debug, Deserialize)]
pub(crate) enum ColourBlindnessType {
Protanopia,
Deuteranopia,
Tritanopia,
All,
}

impl ColourBlindnessType {
pub fn get_colour_palette(self: &ColourBlindnessType) -> ColourPallete {
match self {
ColourBlindnessType::Protanopia => ColourPallete {
number_colours: vec![
Rgb([216, 136, 86]),
Rgb([238, 177, 121]),
Rgb([227, 178, 118]),
],
background_colours: vec![
Rgb([146, 151, 73]),
Rgb([200, 203, 164]),
Rgb([223, 213, 121]),
],
},
ColourBlindnessType::Deuteranopia => ColourPallete {
number_colours: vec![
Rgb([227, 140, 158]),
Rgb([189, 107, 137]),
Rgb([127, 74, 96]),
],
background_colours: vec![
Rgb([96, 94, 83]),
Rgb([161, 158, 141]),
Rgb([85, 78, 62]),
],
},
ColourBlindnessType::Tritanopia => ColourPallete {
number_colours: vec![
Rgb([167, 151, 56]),
Rgb([226, 193, 100]),
Rgb([255, 220, 50]),
],
background_colours: vec![
Rgb([172, 184, 210]),
Rgb([195, 193, 255]),
Rgb([120, 148, 255]),
],
},
ColourBlindnessType::All => ColourPallete {
number_colours: vec![
Rgb([147, 155, 107]),
Rgb([174, 168, 88]),
Rgb([214, 213, 123]),
],
background_colours: vec![
Rgb([232, 130, 93]),
Rgb([229, 128, 90]),
Rgb([236, 180, 137]),
],
},
}
}
}

#[derive(Debug)]
Expand All @@ -22,30 +80,12 @@ struct IshiharaCircle {
pub radius: i32,
}

fn get_colour_palette(ctype: ColourBlindnessType) -> ColourPallete {
match ctype {
ColourBlindnessType::RedGreen => ColourPallete {
number_colours: vec![
Rgb([216, 136, 86]),
Rgb([238, 177, 121]),
Rgb([227, 178, 118]),
],
background_colours: vec![
Rgb([146, 151, 73]),
Rgb([200, 203, 164]),
Rgb([223, 213, 121]),
],
},
// ColourBlindnessType::Green => ColourPallete {
// number_colours: vec![Rgb([219, 226, 139])],
// background_colours: vec![Rgb([121, 185, 38]), Rgb([153, 200, 24])],
// },
}
}

pub fn ishihara_plate(text: &str) -> ImageBuffer<Rgb<u8>, Vec<u8>> {
let canvas_width = 1000;
let canvas_height = 200;
pub fn ishihara_plate(
text: &str,
canvas_width: u32,
canvas_height: u32,
mode: ColourBlindnessType,
) -> ImageBuffer<Rgb<u8>, Vec<u8>> {
let mut img = RgbImage::new(canvas_width, canvas_height);
draw_filled_rect_mut(
&mut img,
Expand All @@ -60,8 +100,11 @@ pub fn ishihara_plate(text: &str) -> ImageBuffer<Rgb<u8>, Vec<u8>> {
let mut bounds = (0, 0);
for c in text.chars().into_iter() {
let new_bounds;
(img, new_bounds) = draw_ishihara_segment(generator.clone(), &font, img, c, bounds, ColourBlindnessType::RedGreen);
bounds = (bounds.0 + new_bounds.max.x as i32 + 5, bounds.1 + new_bounds.max.y as i32);
(img, new_bounds) = draw_ishihara_segment(generator.clone(), &font, img, c, bounds, &mode);
bounds = (
bounds.0 + new_bounds.max.x as i32 + 5,
bounds.1 + new_bounds.max.y as i32,
);
if bounds.0 >= canvas_width - 100 {
bounds = (0, bounds.1 + canvas_height / 2 - 40);
}
Expand All @@ -75,12 +118,12 @@ fn draw_ishihara_segment(
mut img: ImageBuffer<Rgb<u8>, Vec<u8>>,
letter: char,
offset: (i32, i32),
mode: ColourBlindnessType
mode: &ColourBlindnessType,
) -> (ImageBuffer<Rgb<u8>, Vec<u8>>, Rect) {
let circle_min_radius: i32 = 4;
let circle_max_radius: i32 = 10;
let glyph = font.glyph_id(letter).with_scale(200.0);
let colour_palette = get_colour_palette(mode);
let colour_palette = mode.get_colour_palette();
let mut circles: Vec<IshiharaCircle> = vec![];
let mut bounds = Rect::default();
if let Some(outline) = font.outline_glyph(glyph) {
Expand Down
Loading

0 comments on commit 6cea859

Please sign in to comment.