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

Add customization to eye params #39

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
12 changes: 6 additions & 6 deletions examples/custom.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
use fast_qr::{
convert::{image::ImageBuilder, Builder, Shape},
convert::{image::ImageBuilder, Builder, ModuleShape},
ModuleType, QRBuilder, Version, ECL,
};

Expand All @@ -12,21 +12,21 @@ fn main() {

let mut _img = ImageBuilder::default()
// Can have many shapes and custom shapes
.shape(Shape::Command(|y, x, cell| {
.module_shape(ModuleShape::Command(|y, x, cell| {
match cell.module_type() {
ModuleType::FinderPattern | ModuleType::Alignment => String::new(),
_ => {
// Works thanks to Deref
Shape::Square(y, x, cell)
ModuleShape::Square(y, x, cell)
}
}
}))
.shape_color(
Shape::Command(|y, x, cell| {
.module_shape_color(
ModuleShape::Command(|y, x, cell| {
match cell.module_type() {
ModuleType::FinderPattern | ModuleType::Alignment => {
// Works thanks to Deref
Shape::Circle(y, x, cell)
ModuleShape::Circle(y, x, cell)
}
_ => String::new(),
}
Expand Down
4 changes: 2 additions & 2 deletions examples/embed.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
use fast_qr::{
convert::{image::ImageBuilder, Builder, ImageBackgroundShape, Shape},
convert::{image::ImageBuilder, Builder, ImageBackgroundShape, ModuleShape},
QRBuilder, Version, ECL,
};

Expand All @@ -11,7 +11,7 @@ fn main() {
.unwrap();

let mut _img = ImageBuilder::default()
.shape(Shape::Square)
.module_shape(ModuleShape::Square)
.fit_width(600)
.background_color([255, 255, 255, 255])
// New: embed an image
Expand Down
6 changes: 3 additions & 3 deletions examples/image.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
use fast_qr::{
convert::{image::ImageBuilder, Builder, Shape},
convert::{image::ImageBuilder, Builder, ModuleShape},
QRBuilder, Version, ECL,
};

Expand All @@ -11,14 +11,14 @@ fn main() {
.unwrap();

let _image = ImageBuilder::default()
.shape(Shape::RoundedSquare)
.module_shape(ModuleShape::RoundedSquare)
.fit_width(600)
.background_color([255, 255, 255, 0]) // transparency
.to_file(&qrcode, "image.png");

// Or maybe as bytes.
let _image_as_bytes = ImageBuilder::default()
.shape(Shape::RoundedSquare)
.module_shape(ModuleShape::RoundedSquare)
.fit_width(512)
.background_color([255, 255, 255, 255]) // opaque
.to_bytes(&qrcode);
Expand Down
5 changes: 3 additions & 2 deletions examples/svg.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fn main() {
use fast_qr::{
convert::{svg::SvgBuilder, Builder, Shape},
convert::{svg::SvgBuilder, Builder, EyeFrameShape, ModuleShape},
QRBuilder, Version, ECL,
};

Expand All @@ -11,6 +11,7 @@ fn main() {
.unwrap();

let _svg = SvgBuilder::default()
.shape(Shape::RoundedSquare)
.module_shape(ModuleShape::RoundedSquare)
.eye_frame_shape(EyeFrameShape::Square)
.to_file(&qrcode, "svg.svg");
}
76 changes: 76 additions & 0 deletions src/convert/color.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//! Contains functions to convert colors from one format to another

/// Converts an array of pixel color to it's hexadecimal representation
/// # Example
/// ```rust
/// # use fast_qr::convert::rgba2hex;
/// let color = [0, 0, 0, 255];
/// assert_eq!(&rgba2hex(color), "#000000");
/// ```
#[must_use]
pub fn rgba2hex(color: [u8; 4]) -> String {
let mut hex = String::with_capacity(9);

hex.push('#');
hex.push_str(&format!("{:02x}", color[0]));
hex.push_str(&format!("{:02x}", color[1]));
hex.push_str(&format!("{:02x}", color[2]));
if color[3] != 255 {
hex.push_str(&format!("{:02x}", color[3]));
}

hex
}

/// Allows to take String, string slices, arrays or slices of u8 (3 or 4) to create a [Color]
pub struct Color(pub String);

impl Color {
/// Returns the contained color
#[must_use]
pub fn to_str(&self) -> &str {
&self.0
}
}

impl From<String> for Color {
fn from(color: String) -> Self {
Self(color)
}
}

impl From<&str> for Color {
fn from(color: &str) -> Self {
Self(color.to_string())
}
}

impl From<[u8; 4]> for Color {
fn from(color: [u8; 4]) -> Self {
Self(rgba2hex(color))
}
}

impl From<[u8; 3]> for Color {
fn from(color: [u8; 3]) -> Self {
Self::from([color[0], color[1], color[2], 255])
}
}

impl From<&[u8]> for Color {
fn from(color: &[u8]) -> Self {
if color.len() == 3 {
Self::from([color[0], color[1], color[2]])
} else if color.len() == 4 {
Self::from([color[0], color[1], color[2], color[3]])
} else {
panic!("Invalid color length");
}
}
}

impl From<Vec<u8>> for Color {
fn from(color: Vec<u8>) -> Self {
Self::from(&color[..])
}
}
Loading
Loading