Skip to content
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
17 changes: 17 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "cargo"
directory: "/"
schedule:
interval: "weekly"

# https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot#example-dependabotyml-file-for-github-actions=
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
name: cargo check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand All @@ -18,7 +18,7 @@ jobs:
name: cargo test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand All @@ -31,7 +31,7 @@ jobs:
name: cargo fmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ build-bin = ["md-5"]
name = "identicon"

[dependencies]
image = { version = "0.23.14", default-features = false, features = ["png"] }
md-5 = { version = "0.9.1", features = ["asm"], optional = true }
image = { version = "0.25.0", default-features = false, features = ["png"] }
md-5 = { version = "0.10", optional = true }

[[bin]]
name = "identicon"
Expand Down
30 changes: 13 additions & 17 deletions src/hsl.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use image::Rgb;

pub struct HSL {
pub struct Hsl {
hue: f32,
sat: f32,
lum: f32,
}

impl HSL {
pub fn new(hue: f32, sat: f32, lum: f32) -> HSL {
HSL {
hue: hue,
sat: sat,
lum: lum,
}
impl Hsl {
pub fn new(hue: f32, sat: f32, lum: f32) -> Hsl {
Hsl { hue, sat, lum }
}

// http://www.w3.org/TR/css3-color/#hsl-color
Expand All @@ -28,9 +24,9 @@ impl HSL {
};
let a = lum * 2.0 - b;

let r = HSL::hue_to_rgb(a, b, hue + 1.0 / 3.0);
let g = HSL::hue_to_rgb(a, b, hue);
let b = HSL::hue_to_rgb(a, b, hue - 1.0 / 3.0);
let r = Hsl::hue_to_rgb(a, b, hue + 1.0 / 3.0);
let g = Hsl::hue_to_rgb(a, b, hue);
let b = Hsl::hue_to_rgb(a, b, hue - 1.0 / 3.0);

Rgb([
(r * 255.0).round() as u8,
Expand Down Expand Up @@ -66,41 +62,41 @@ impl HSL {

#[cfg(test)]
mod tests {
use super::HSL;
use super::Hsl;
use image::Rgb;

#[test]
fn it_converts_black() {
let black = Rgb([0, 0, 0]);
let rgb = HSL::new(0.0, 0.0, 0.0).rgb();
let rgb = Hsl::new(0.0, 0.0, 0.0).rgb();
assert_eq!(black, rgb);
}

#[test]
fn it_converts_white() {
let white = Rgb([255, 255, 255]);
let rgb = HSL::new(0.0, 0.0, 100.0).rgb();
let rgb = Hsl::new(0.0, 0.0, 100.0).rgb();
assert_eq!(white, rgb);
}

#[test]
fn it_converts_red() {
let red = Rgb([255, 0, 0]);
let rgb = HSL::new(0.0, 100.0, 50.0).rgb();
let rgb = Hsl::new(0.0, 100.0, 50.0).rgb();
assert_eq!(red, rgb);
}

#[test]
fn it_converts_green() {
let green = Rgb([0, 255, 0]);
let rgb = HSL::new(120.0, 100.0, 50.0).rgb();
let rgb = Hsl::new(120.0, 100.0, 50.0).rgb();
assert_eq!(green, rgb);
}

#[test]
fn it_converts_blue() {
let blue = Rgb([0, 0, 255]);
let rgb = HSL::new(240.0, 100.0, 50.0).rgb();
let rgb = Hsl::new(240.0, 100.0, 50.0).rgb();
assert_eq!(blue, rgb);
}
}
11 changes: 4 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use image::{ImageBuffer, Rgb, RgbImage};

use hsl::HSL;
use hsl::Hsl;
use nibbler::Nibbler;

mod hsl;
Expand All @@ -13,10 +13,7 @@ pub struct Identicon<'a> {

impl<'a> Identicon<'a> {
pub fn new(source: &[u8]) -> Identicon {
Identicon {
source: source,
size: 420,
}
Identicon { source, size: 420 }
}

// https://processing.org/reference/map_.html
Expand All @@ -25,7 +22,7 @@ impl<'a> Identicon<'a> {
}

fn foreground(&self) -> Rgb<u8> {
// Use last 28 bits to determine HSL values.
// Use last 28 bits to determine Hsl values.
let h1 = (self.source[12] as u16 & 0x0f) << 8;
let h2 = self.source[13] as u16;

Expand All @@ -37,7 +34,7 @@ impl<'a> Identicon<'a> {
let sat = Identicon::map(s, 0, 255, 0, 20);
let lum = Identicon::map(l, 0, 255, 0, 20);

HSL::new(hue, 65.0 - sat, 75.0 - lum).rgb()
Hsl::new(hue, 65.0 - sat, 75.0 - lum).rgb()
}

fn rect(image: &mut RgbImage, x0: u32, y0: u32, x1: u32, y1: u32, color: Rgb<u8>) {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::io;
use std::io::Result;
use std::process::exit;

use image::png::PngEncoder;
use image::ColorType;
use image::{codecs::png::PngEncoder, ImageEncoder};
use md5::{Digest, Md5};

use identicon::Identicon;
Expand All @@ -27,7 +27,7 @@ fn generate(input: &[u8]) -> Result<()> {
let output = &mut io::stdout();
let encoder = PngEncoder::new(output);
encoder
.encode(image.as_ref(), width, height, ColorType::Rgb8)
.write_image(image.as_ref(), width, height, ColorType::Rgb8.into())
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}

Expand Down