| 1 | //! Converts a [`crate::QRCode`] to image or SVG you will need to activate associated feature flag |
| 2 | |
| 3 | #[cfg(feature = "svg")] |
| 4 | #[cfg_attr(docsrs, doc(cfg(feature = "svg")))] |
| 5 | pub mod svg; |
| 6 | use core::ops::Deref; |
| 7 | |
| 8 | #[cfg(feature = "svg")] |
| 9 | use svg::SvgError; |
| 10 | |
| 11 | #[cfg(feature = "image")] |
| 12 | #[cfg_attr(docsrs, doc(cfg(feature = "image")))] |
| 13 | pub mod image; |
| 14 | #[cfg(feature = "image")] |
| 15 | use image::ImageError; |
| 16 | |
| 17 | use crate::Module; |
| 18 | |
| 19 | /// Converts a position to a module svg |
| 20 | /// # Example |
| 21 | /// |
| 22 | /// For the square shape, the svg is `M{x},{y}h1v1h-1` |
| 23 | /// |
| 24 | /// ```rust |
| 25 | /// # use fast_qr::Module; |
| 26 | /// fn square(y: usize, x: usize, _module: Module) -> String { |
| 27 | /// format!("M{x},{y}h1v1h-1") |
| 28 | /// } |
| 29 | /// ``` |
| 30 | pub type ModuleFunction = fn(usize, usize, Module) -> String; |
| 31 | |
| 32 | #[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))] |
| 33 | use wasm_bindgen::prelude::*; |
| 34 | |
| 35 | /// Different possible Shapes to represent modules in a [`crate::QRCode`] |
| 36 | #[repr(C)] |
| 37 | #[wasm_bindgen] |
| 38 | #[cfg(feature = "wasm-bindgen")] |
| 39 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)] |
| 40 | pub enum Shape { |
| 41 | /// Square Shape |
| 42 | Square, |
| 43 | /// Circle Shape |
| 44 | Circle, |
| 45 | /// RoundedSquare Shape |
| 46 | RoundedSquare, |
| 47 | /// Vertical Shape |
| 48 | Vertical, |
| 49 | /// Horizontal Shape |
| 50 | Horizontal, |
| 51 | /// Diamond Shape |
| 52 | Diamond, |
| 53 | } |
| 54 | |
| 55 | /// Different possible Shapes to represent modules in a [`crate::QRCode`] |
| 56 | #[cfg(not(feature = "wasm-bindgen"))] |
| 57 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)] |
| 58 | pub enum Shape { |
| 59 | /// Square Shape |
| 60 | Square, |
| 61 | /// Circle Shape |
| 62 | Circle, |
| 63 | /// RoundedSquare Shape |
| 64 | RoundedSquare, |
| 65 | /// Vertical Shape |
| 66 | Vertical, |
| 67 | /// Horizontal Shape |
| 68 | Horizontal, |
| 69 | /// Diamond Shape |
| 70 | Diamond, |
| 71 | /// Custom Shape with a function / closure |
| 72 | /// # Example |
| 73 | /// ```rust |
| 74 | /// use fast_qr::convert::Shape; |
| 75 | /// let command_function = |y, x, cell| { |
| 76 | /// if x % 2 == 0 { |
| 77 | /// // Works thanks to Deref |
| 78 | /// Shape::Square(y, x, cell) |
| 79 | /// } else { |
| 80 | /// // Rectangle |
| 81 | /// format!("M{x},{y}h1v.5h-1") |
| 82 | /// } |
| 83 | /// }; |
| 84 | /// let command = Shape::Command(command_function); |
| 85 | /// ``` |
| 86 | /// |
| 87 | /// <svg viewBox="0 0 37 37" xmlns="http://www.w3.org/2000/svg" width="250px"> |
| 88 | /// <rect width="37px" height="37px" fill="#ffffff" /> |
| 89 | /// <path |
| 90 | /// d="M4,4h1v1h-1M4,5h1v1h-1M4,6h1v1h-1M4,7h1v1h-1M4,8h1v1h-1M4,9h1v1h-1M4,10h1v1h-1M4,12h1v1h-1M4,13h1v1h-1M4,17h1v1h-1M4,19h1v1h-1M4,22h1v1h-1M4,24h1v1h-1M4,26h1v1h-1M4,27h1v1h-1M4,28h1v1h-1M4,29h1v1h-1M4,30h1v1h-1M4,31h1v1h-1M4,32h1v1h-1M5,4h1v.5h-1M5,10h1v.5h-1M5,12h1v.5h-1M5,13h1v.5h-1M5,14h1v.5h-1M5,17h1v.5h-1M5,19h1v.5h-1M5,22h1v.5h-1M5,23h1v.5h-1M5,26h1v.5h-1M5,32h1v.5h-1M6,4h1v1h-1M6,6h1v1h-1M6,7h1v1h-1M6,8h1v1h-1M6,10h1v1h-1M6,12h1v1h-1M6,14h1v1h-1M6,16h1v1h-1M6,18h1v1h-1M6,19h1v1h-1M6,23h1v1h-1M6,24h1v1h-1M6,26h1v1h-1M6,28h1v1h-1M6,29h1v1h-1M6,30h1v1h-1M6,32h1v1h-1M7,4h1v.5h-1M7,6h1v.5h-1M7,7h1v.5h-1M7,8h1v.5h-1M7,10h1v.5h-1M7,13h1v.5h-1M7,15h1v.5h-1M7,18h1v.5h-1M7,21h1v.5h-1M7,23h1v.5h-1M7,26h1v.5h-1M7,28h1v.5h-1M7,29h1v.5h-1M7,30h1v.5h-1M7,32h1v.5h-1M8,4h1v1h-1M8,6h1v1h-1M8,7h1v1h-1M8,8h1v1h-1M8,10h1v1h-1M8,16h1v1h-1M8,17h1v1h-1M8,18h1v1h-1M8,19h1v1h-1M8,20h1v1h-1M8,22h1v1h-1M8,23h1v1h-1M8,24h1v1h-1M8,26h1v1h-1M8,28h1v1h-1M8,29h1v1h-1M8,30h1v1h-1M8,32h1v1h-1M9,4h1v.5h-1M9,10h1v.5h-1M9,12h1v.5h-1M9,13h1v.5h-1M9,14h1v.5h-1M9,15h1v.5h-1M9,16h1v.5h-1M9,19h1v.5h-1M9,22h1v.5h-1M9,26h1v.5h-1M9,32h1v.5h-1M10,4h1v1h-1M10,5h1v1h-1M10,6h1v1h-1M10,7h1v1h-1M10,8h1v1h-1M10,9h1v1h-1M10,10h1v1h-1M10,12h1v1h-1M10,14h1v1h-1M10,16h1v1h-1M10,18h1v1h-1M10,20h1v1h-1M10,22h1v1h-1M10,24h1v1h-1M10,26h1v1h-1M10,27h1v1h-1M10,28h1v1h-1M10,29h1v1h-1M10,30h1v1h-1M10,31h1v1h-1M10,32h1v1h-1M11,12h1v.5h-1M11,13h1v.5h-1M11,15h1v.5h-1M11,16h1v.5h-1M11,17h1v.5h-1M11,18h1v.5h-1M11,19h1v.5h-1M12,6h1v1h-1M12,7h1v1h-1M12,8h1v1h-1M12,10h1v1h-1M12,12h1v1h-1M12,20h1v1h-1M12,22h1v1h-1M12,23h1v1h-1M12,24h1v1h-1M12,25h1v1h-1M12,26h1v1h-1M12,27h1v1h-1M12,30h1v1h-1M12,31h1v1h-1M12,32h1v1h-1M13,9h1v.5h-1M13,11h1v.5h-1M13,12h1v.5h-1M13,13h1v.5h-1M13,14h1v.5h-1M13,15h1v.5h-1M13,16h1v.5h-1M13,18h1v.5h-1M13,20h1v.5h-1M13,25h1v.5h-1M13,26h1v.5h-1M13,27h1v.5h-1M13,28h1v.5h-1M13,29h1v.5h-1M13,30h1v.5h-1M13,32h1v.5h-1M14,4h1v1h-1M14,6h1v1h-1M14,7h1v1h-1M14,9h1v1h-1M14,10h1v1h-1M14,12h1v1h-1M14,13h1v1h-1M14,14h1v1h-1M14,15h1v1h-1M14,16h1v1h-1M14,17h1v1h-1M14,18h1v1h-1M14,19h1v1h-1M14,20h1v1h-1M14,22h1v1h-1M14,24h1v1h-1M14,25h1v1h-1M14,26h1v1h-1M14,27h1v1h-1M15,4h1v.5h-1M15,6h1v.5h-1M15,8h1v.5h-1M15,9h1v.5h-1M15,11h1v.5h-1M15,12h1v.5h-1M15,13h1v.5h-1M15,15h1v.5h-1M15,16h1v.5h-1M15,18h1v.5h-1M15,20h1v.5h-1M15,21h1v.5h-1M15,22h1v.5h-1M15,25h1v.5h-1M15,26h1v.5h-1M15,27h1v.5h-1M15,29h1v.5h-1M15,31h1v.5h-1M16,5h1v1h-1M16,7h1v1h-1M16,9h1v1h-1M16,10h1v1h-1M16,11h1v1h-1M16,12h1v1h-1M16,14h1v1h-1M16,17h1v1h-1M16,24h1v1h-1M16,25h1v1h-1M16,27h1v1h-1M16,30h1v1h-1M16,31h1v1h-1M16,32h1v1h-1M17,5h1v.5h-1M17,6h1v.5h-1M17,8h1v.5h-1M17,9h1v.5h-1M17,12h1v.5h-1M17,16h1v.5h-1M17,18h1v.5h-1M17,20h1v.5h-1M17,23h1v.5h-1M17,24h1v.5h-1M17,25h1v.5h-1M17,26h1v.5h-1M17,28h1v.5h-1M17,29h1v.5h-1M17,31h1v.5h-1M17,32h1v.5h-1M18,4h1v1h-1M18,5h1v1h-1M18,7h1v1h-1M18,9h1v1h-1M18,10h1v1h-1M18,12h1v1h-1M18,13h1v1h-1M18,14h1v1h-1M18,16h1v1h-1M18,19h1v1h-1M18,20h1v1h-1M18,22h1v1h-1M18,24h1v1h-1M18,26h1v1h-1M18,27h1v1h-1M19,4h1v.5h-1M19,6h1v.5h-1M19,7h1v.5h-1M19,8h1v.5h-1M19,12h1v.5h-1M19,13h1v.5h-1M19,16h1v.5h-1M19,21h1v.5h-1M19,22h1v.5h-1M19,24h1v.5h-1M19,28h1v.5h-1M19,29h1v.5h-1M19,31h1v.5h-1M20,5h1v1h-1M20,6h1v1h-1M20,8h1v1h-1M20,9h1v1h-1M20,10h1v1h-1M20,13h1v1h-1M20,14h1v1h-1M20,16h1v1h-1M20,19h1v1h-1M20,20h1v1h-1M20,25h1v1h-1M20,29h1v1h-1M20,30h1v1h-1M20,31h1v1h-1M21,4h1v.5h-1M21,6h1v.5h-1M21,7h1v.5h-1M21,8h1v.5h-1M21,12h1v.5h-1M21,14h1v.5h-1M21,16h1v.5h-1M21,17h1v.5h-1M21,19h1v.5h-1M21,20h1v.5h-1M21,24h1v.5h-1M21,25h1v.5h-1M21,26h1v.5h-1M21,27h1v.5h-1M21,28h1v.5h-1M21,29h1v.5h-1M21,31h1v.5h-1M21,32h1v.5h-1M22,4h1v1h-1M22,7h1v1h-1M22,8h1v1h-1M22,10h1v1h-1M22,13h1v1h-1M22,15h1v1h-1M22,17h1v1h-1M22,19h1v1h-1M22,20h1v1h-1M22,21h1v1h-1M22,23h1v1h-1M22,26h1v1h-1M22,27h1v1h-1M22,29h1v1h-1M23,4h1v.5h-1M23,6h1v.5h-1M23,9h1v.5h-1M23,11h1v.5h-1M23,13h1v.5h-1M23,14h1v.5h-1M23,15h1v.5h-1M23,16h1v.5h-1M23,19h1v.5h-1M23,20h1v.5h-1M23,21h1v.5h-1M23,23h1v.5h-1M23,24h1v.5h-1M23,26h1v.5h-1M23,28h1v.5h-1M23,31h1v.5h-1M24,4h1v1h-1M24,6h1v1h-1M24,7h1v1h-1M24,9h1v1h-1M24,10h1v1h-1M24,12h1v1h-1M24,14h1v1h-1M24,15h1v1h-1M24,16h1v1h-1M24,17h1v1h-1M24,18h1v1h-1M24,19h1v1h-1M24,20h1v1h-1M24,22h1v1h-1M24,23h1v1h-1M24,24h1v1h-1M24,25h1v1h-1M24,26h1v1h-1M24,27h1v1h-1M24,28h1v1h-1M24,30h1v1h-1M25,12h1v.5h-1M25,16h1v.5h-1M25,18h1v.5h-1M25,20h1v.5h-1M25,21h1v.5h-1M25,22h1v.5h-1M25,24h1v.5h-1M25,28h1v.5h-1M25,29h1v.5h-1M25,32h1v.5h-1M26,4h1v1h-1M26,5h1v1h-1M26,6h1v1h-1M26,7h1v1h-1M26,8h1v1h-1M26,9h1v1h-1M26,10h1v1h-1M26,14h1v1h-1M26,16h1v1h-1M26,17h1v1h-1M26,18h1v1h-1M26,19h1v1h-1M26,21h1v1h-1M26,22h1v1h-1M26,23h1v1h-1M26,24h1v1h-1M26,26h1v1h-1M26,28h1v1h-1M27,4h1v.5h-1M27,10h1v.5h-1M27,13h1v.5h-1M27,14h1v.5h-1M27,15h1v.5h-1M27,16h1v.5h-1M27,17h1v.5h-1M27,19h1v.5h-1M27,20h1v.5h-1M27,22h1v.5h-1M27,23h1v.5h-1M27,24h1v.5h-1M27,28h1v.5h-1M27,29h1v.5h-1M28,4h1v1h-1M28,6h1v1h-1M28,7h1v1h-1M28,8h1v1h-1M28,10h1v1h-1M28,12h1v1h-1M28,13h1v1h-1M28,16h1v1h-1M28,20h1v1h-1M28,21h1v1h-1M28,22h1v1h-1M28,24h1v1h-1M28,25h1v1h-1M28,26h1v1h-1M28,27h1v1h-1M28,28h1v1h-1M28,29h1v1h-1M28,30h1v1h-1M28,32h1v1h-1M29,4h1v.5h-1M29,6h1v.5h-1M29,7h1v.5h-1M29,8h1v.5h-1M29,10h1v.5h-1M29,12h1v.5h-1M29,13h1v.5h-1M29,15h1v.5h-1M29,16h1v.5h-1M29,17h1v.5h-1M29,18h1v.5h-1M29,22h1v.5h-1M29,23h1v.5h-1M29,24h1v.5h-1M29,25h1v.5h-1M29,27h1v.5h-1M29,29h1v.5h-1M29,30h1v.5h-1M30,4h1v1h-1M30,6h1v1h-1M30,7h1v1h-1M30,8h1v1h-1M30,10h1v1h-1M30,12h1v1h-1M30,13h1v1h-1M30,14h1v1h-1M30,16h1v1h-1M30,18h1v1h-1M30,20h1v1h-1M30,21h1v1h-1M30,22h1v1h-1M30,23h1v1h-1M30,24h1v1h-1M30,25h1v1h-1M30,26h1v1h-1M30,27h1v1h-1M30,28h1v1h-1M30,30h1v1h-1M30,31h1v1h-1M31,4h1v.5h-1M31,10h1v.5h-1M31,13h1v.5h-1M31,18h1v.5h-1M31,19h1v.5h-1M31,20h1v.5h-1M31,21h1v.5h-1M31,26h1v.5h-1M31,28h1v.5h-1M31,29h1v.5h-1M31,31h1v.5h-1M32,4h1v1h-1M32,5h1v1h-1M32,6h1v1h-1M32,7h1v1h-1M32,8h1v1h-1M32,9h1v1h-1M32,10h1v1h-1M32,14h1v1h-1M32,15h1v1h-1M32,16h1v1h-1M32,17h1v1h-1M32,18h1v1h-1M32,19h1v1h-1M32,22h1v1h-1M32,26h1v1h-1M32,28h1v1h-1M32,30h1v1h-1" |
| 91 | /// fill="#000000" /> |
| 92 | /// </svg> |
| 93 | Command(ModuleFunction), |
| 94 | } |
| 95 | |
| 96 | impl From<Shape> for usize { |
| 97 | fn from(shape: Shape) -> Self { |
| 98 | match shape { |
| 99 | Shape::Square => 0, |
| 100 | Shape::Circle => 1, |
| 101 | Shape::RoundedSquare => 2, |
| 102 | Shape::Vertical => 3, |
| 103 | Shape::Horizontal => 4, |
| 104 | Shape::Diamond => 5, |
| 105 | #[cfg(not(feature = "wasm-bindgen"))] |
| 106 | Shape::Command(_) => 6, |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | impl From<String> for Shape { |
| 112 | #[allow(clippy::match_same_arms)] |
| 113 | fn from(shape: String) -> Self { |
| 114 | match shape.to_lowercase().as_str() { |
| 115 | "square" => Shape::Square, |
| 116 | "circle" => Shape::Circle, |
| 117 | "rounded_square" => Shape::RoundedSquare, |
| 118 | "vertical" => Shape::Vertical, |
| 119 | "horizontal" => Shape::Horizontal, |
| 120 | "diamond" => Shape::Diamond, |
| 121 | |
| 122 | _ => Shape::Square, |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | impl From<Shape> for &str { |
| 128 | fn from(shape: Shape) -> Self { |
| 129 | match shape { |
| 130 | Shape::Square => "square", |
| 131 | Shape::Circle => "circle", |
| 132 | Shape::RoundedSquare => "rounded_square", |
| 133 | Shape::Vertical => "vertical", |
| 134 | Shape::Horizontal => "horizontal", |
| 135 | Shape::Diamond => "diamond", |
| 136 | #[cfg(not(feature = "wasm-bindgen"))] |
| 137 | Shape::Command(_) => "command", |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl Shape { |
| 143 | pub(crate) fn square(y: usize, x: usize, _: Module) -> String { |
| 144 | format!("M{x},{y}h1v1h-1") |
| 145 | } |
| 146 | |
| 147 | pub(crate) fn circle(y: usize, x: usize, _: Module) -> String { |
| 148 | format!("M{},{y}.5a.5,.5 0 1,1 0,-.1", x + 1) |
| 149 | } |
| 150 | |
| 151 | pub(crate) fn rounded_square(y: usize, x: usize, _: Module) -> String { |
| 152 | format!("M{x}.2,{y}.2 {x}.8,{y}.2 {x}.8,{y}.8 {x}.2,{y}.8z") |
| 153 | } |
| 154 | |
| 155 | pub(crate) fn horizontal(y: usize, x: usize, _: Module) -> String { |
| 156 | format!("M{x},{y}.1h1v.8h-1") |
| 157 | } |
| 158 | |
| 159 | pub(crate) fn vertical(y: usize, x: usize, _: Module) -> String { |
| 160 | format!("M{x}.1,{y}h.8v1h-.8") |
| 161 | } |
| 162 | |
| 163 | pub(crate) fn diamond(y: usize, x: usize, _: Module) -> String { |
| 164 | format!("M{x}.5,{y}l.5,.5l-.5,.5l-.5,-.5z") |
| 165 | } |
| 166 | |
| 167 | const FUNCTIONS: [ModuleFunction; 6] = [ |
| 168 | Shape::square, |
| 169 | Shape::circle, |
| 170 | Shape::rounded_square, |
| 171 | Shape::vertical, |
| 172 | Shape::horizontal, |
| 173 | Shape::diamond, |
| 174 | ]; |
| 175 | } |
| 176 | |
| 177 | impl Deref for Shape { |
| 178 | type Target = ModuleFunction; |
| 179 | |
| 180 | fn deref(&self) -> &Self::Target { |
| 181 | let index: usize = (*self).into(); |
| 182 | match self { |
| 183 | #[cfg(not(feature = "wasm-bindgen"))] |
| 184 | Self::Command(func) => func, |
| 185 | _ => &Self::FUNCTIONS[index], |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /// Different possible image background shapes |
| 191 | #[cfg_attr(feature = "wasm-bindgen", repr(C), wasm_bindgen)] |
| 192 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)] |
| 193 | pub enum ImageBackgroundShape { |
| 194 | /// Square shape |
| 195 | Square, |
| 196 | /// Circle shape |
| 197 | Circle, |
| 198 | /// Rounded square shape |
| 199 | RoundedSquare, |
| 200 | } |
| 201 | |
| 202 | /// Contains possible errors for a conversion |
| 203 | #[derive(Debug)] |
| 204 | pub enum ConvertError { |
| 205 | /// Contains error message for a SVG conversion |
| 206 | #[cfg(feature = "svg")] |
| 207 | #[cfg_attr(docsrs, doc(cfg(feature = "svg")))] |
| 208 | Svg(String), |
| 209 | /// Contains error message for an Image conversion |
| 210 | #[cfg(feature = "image")] |
| 211 | #[cfg_attr(docsrs, doc(cfg(feature = "image")))] |
| 212 | Image(String), |
| 213 | /// Contains error message if a file write failed |
| 214 | Io(std::io::Error), |
| 215 | } |
| 216 | |
| 217 | #[cfg(feature = "svg")] |
| 218 | #[cfg_attr(docsrs, doc(cfg(feature = "svg")))] |
| 219 | impl From<SvgError> for ConvertError { |
| 220 | fn from(err: SvgError) -> Self { |
| 221 | match err { |
| 222 | SvgError::SvgError(svg_err) => Self::Svg(svg_err), |
| 223 | #[cfg(not(feature = "wasm-bindgen"))] |
| 224 | SvgError::IoError(io_err) => Self::Io(io_err), |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | #[cfg(feature = "image")] |
| 230 | #[cfg_attr(docsrs, doc(cfg(feature = "image")))] |
| 231 | impl From<ImageError> for ConvertError { |
| 232 | fn from(err: ImageError) -> Self { |
| 233 | match err { |
| 234 | ImageError::EncodingError(image_err) => Self::Image(image_err), |
| 235 | ImageError::ImageError(image_err) => Self::Image(image_err), |
| 236 | ImageError::IoError(io_err) => Self::Io(io_err), |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /// Converts an array of pixel color to it's hexadecimal representation |
| 242 | /// # Example |
| 243 | /// ```rust |
| 244 | /// # use fast_qr::convert::rgba2hex; |
| 245 | /// let color = [0, 0, 0, 255]; |
| 246 | /// assert_eq!(&rgba2hex(color), "#000000"); |
| 247 | /// ``` |
| 248 | #[must_use] |
| 249 | pub fn rgba2hex(color: [u8; 4]) -> String { |
| 250 | let mut hex = String::with_capacity(9); |
| 251 | |
| 252 | hex.push('#'); |
| 253 | hex.push_str(&format!("{:02x}", color[0])); |
| 254 | hex.push_str(&format!("{:02x}", color[1])); |
| 255 | hex.push_str(&format!("{:02x}", color[2])); |
| 256 | if color[3] != 255 { |
| 257 | hex.push_str(&format!("{:02x}", color[3])); |
| 258 | } |
| 259 | |
| 260 | hex |
| 261 | } |
| 262 | |
| 263 | /// Allows to take String, string slices, arrays or slices of u8 (3 or 4) to create a [Color] |
| 264 | pub struct Color(pub String); |
| 265 | |
| 266 | impl Color { |
| 267 | /// Returns the contained color |
| 268 | #[must_use] |
| 269 | pub fn to_str(&self) -> &str { |
| 270 | &self.0 |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | impl From<String> for Color { |
| 275 | fn from(color: String) -> Self { |
| 276 | Self(color) |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | impl From<&str> for Color { |
| 281 | fn from(color: &str) -> Self { |
| 282 | Self(color.to_string()) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | impl From<[u8; 4]> for Color { |
| 287 | fn from(color: [u8; 4]) -> Self { |
| 288 | Self(rgba2hex(color)) |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | impl From<[u8; 3]> for Color { |
| 293 | fn from(color: [u8; 3]) -> Self { |
| 294 | Self::from([color[0], color[1], color[2], 255]) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | impl From<&[u8]> for Color { |
| 299 | fn from(color: &[u8]) -> Self { |
| 300 | if color.len() == 3 { |
| 301 | Self::from([color[0], color[1], color[2]]) |
| 302 | } else if color.len() == 4 { |
| 303 | Self::from([color[0], color[1], color[2], color[3]]) |
| 304 | } else { |
| 305 | panic!("Invalid color length"); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | impl From<Vec<u8>> for Color { |
| 311 | fn from(color: Vec<u8>) -> Self { |
| 312 | Self::from(&color[..]) |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /// Trait for `SvgBuilder` and `ImageBuilder` |
| 317 | pub trait Builder { |
| 318 | /// Updates margin (default: 4) |
| 319 | fn margin(&mut self, margin: usize) -> &mut Self; |
| 320 | /// Updates module color (default: #000000) |
| 321 | fn module_color<C: Into<Color>>(&mut self, module_color: C) -> &mut Self; |
| 322 | /// Updates background color (default: #FFFFFF) |
| 323 | fn background_color<C: Into<Color>>(&mut self, background_color: C) -> &mut Self; |
| 324 | /// Adds a shape to the shapes list |
| 325 | fn shape(&mut self, shape: Shape) -> &mut Self; |
| 326 | /// Add a shape to the shapes list with a specific color |
| 327 | fn shape_color<C: Into<Color>>(&mut self, shape: Shape, color: C) -> &mut Self; |
| 328 | |
| 329 | // Manages the image part |
| 330 | |
| 331 | /// Provides the image path or an base64 encoded image |
| 332 | fn image(&mut self, image: String) -> &mut Self; |
| 333 | /// Updates the image background color (default: #FFFFFF) |
| 334 | fn image_background_color<C: Into<Color>>(&mut self, image_background_color: C) -> &mut Self; |
| 335 | /// Updates the image background shape (default: Square) |
| 336 | fn image_background_shape(&mut self, image_background_shape: ImageBackgroundShape) |
| 337 | -> &mut Self; |
| 338 | /// Updates the image size and the gap between the image and the [`crate::QRCode`] |
| 339 | /// Default is around 30% of the [`crate::QRCode`] size |
| 340 | fn image_size(&mut self, image_size: f64) -> &mut Self; |
| 341 | /// Updates the gap between the image and the [`crate::QRCode`] |
| 342 | fn image_gap(&mut self, gap: f64) -> &mut Self; |
| 343 | /// Updates the image position, anchor is the center of the image. Default is the center of the [`crate::QRCode`] |
| 344 | fn image_position(&mut self, x: f64, y: f64) -> &mut Self; |
| 345 | } |