Skip to content

Commit

Permalink
now generating colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Inspirateur committed Dec 3, 2022
1 parent 3d70356 commit d8fe65a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 11 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ palette = "*"
serenity = "*"
anyhow = "*"
itertools = "*"
rand = "*"
rand = "*"
lazy_static = "*"
8 changes: 6 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod wordcloud;
use serenity::utils::Color;
use wordcloud::{wordcloud, Token};

fn test_tokens() -> Vec<(Token, f32)> {
Expand All @@ -7,7 +8,10 @@ fn test_tokens() -> Vec<(Token, f32)> {
(Token::IMG("assets/images/kingsip.webp".to_string()), 0.005),
(Token::TEXT("KeK'".to_string()), 0.02),
(Token::TEXT("Sauce".to_string()), 0.01),
(Token::TEXT("Polisson".to_string()), 0.005)
(Token::TEXT("Polisson".to_string()), 0.005),
(Token::TEXT("C'est".to_string()), 0.005),
(Token::TEXT("la".to_string()), 0.005),
(Token::TEXT("fête".to_string()), 0.005)
]
}

Expand All @@ -16,6 +20,6 @@ fn main() {
// Parse it into the font type.
let font = fontdue::Font::from_bytes(font, fontdue::FontSettings::default()).unwrap();

let wc = wordcloud(font, (800, 400), test_tokens(), None);
let wc = wordcloud(font, (800, 400), test_tokens(), Some(Color::from_rgb(220, 240, 60)));
wc.save("test.png").unwrap();
}
43 changes: 37 additions & 6 deletions src/wordcloud/colors.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
use palette::rgb::Rgba;
use image::Rgba as IRgba;
use lazy_static::lazy_static;
use palette::{rgb::Rgba, FromColor, Lcha, Hue};
use image::{Rgba as IRgba};
use serenity::utils::Color;
use rand::{self, rngs::ThreadRng, distributions::Uniform, prelude::Distribution};

pub struct Colors {
const ANGLES: [i32; 5] = [
-15, 0, 15, 180-15, 180+15
];

lazy_static! {
static ref INDEXES: Uniform<usize> = Uniform::from(0..ANGLES.len());
static ref H_NOISE: Uniform<f32> = Uniform::from(-2.0..2.);
static ref C_NOISE: Uniform<f32> = Uniform::from(-10.0..10.);
}

pub struct Colors {
anchor: Lcha,
rng: ThreadRng
}

fn convert_color(color: Rgba) -> IRgba<u8> {
Expand All @@ -16,11 +29,29 @@ fn convert_color(color: Rgba) -> IRgba<u8> {
}

impl Colors {
pub fn new(color: Option<Color>) -> Self {
Self { }
pub fn new(color_opt: Option<Color>) -> Self {
let color: Rgba = match color_opt {
Some(color) => Rgba::new(
color.r() as f32/255.,
color.g() as f32/255.,
color.b() as f32/255.,
1.
),
None => Rgba::new(1., 1., 1., 1.)
};
let mut anchor = Lcha::from_color(color);
anchor.chroma = anchor.chroma.max(20.);
Self {
rng: rand::thread_rng(),
anchor
}
}

pub fn get(&mut self) -> IRgba<u8> {
convert_color(Rgba::new(0.7, 0.4, 0.1, 1.0))
let angle = ANGLES[INDEXES.sample(&mut self.rng)] as f32;
let hue_shift = angle+H_NOISE.sample(&mut self.rng);
let mut new_color = self.anchor.shift_hue(hue_shift);
new_color.chroma += C_NOISE.sample(&mut self.rng);
convert_color(Rgba::from_color(new_color))
}
}
3 changes: 2 additions & 1 deletion src/wordcloud/image.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Display;
use image::{open, imageops::FilterType, GenericImage, RgbaImage, DynamicImage, GenericImageView};
use itertools::Itertools;
use std::path::Path;
use super::{rasterisable::Rasterisable, hxbitmap::HXBitmap};

pub struct Image {
Expand All @@ -19,7 +20,7 @@ impl Image {

impl Display for Image {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.path)
write!(f, "{:?}", Path::new(&self.path).file_name())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/wordcloud/wordcloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl WorldCloud {
match self.bitmap.place(bitmap) {
Ok(pos) => {
token.draw(&mut self.image, pos);
println!("Placed {} at {:?}", token, pos);
println!("Placed `{}` at {:?}", token, pos);
true
},
Err(err) => {
Expand Down
Binary file modified test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d8fe65a

Please sign in to comment.