├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── LICENSE ├── README.md ├── examples └── aface.rs ├── face.svg └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | *.swp 3 | -------------------------------------------------------------------------------- /Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "cfg-if" 5 | version = "0.1.10" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" 8 | 9 | [[package]] 10 | name = "getrandom" 11 | version = "0.2.0" 12 | source = "registry+https://github.com/rust-lang/crates.io-index" 13 | checksum = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4" 14 | dependencies = [ 15 | "cfg-if", 16 | "libc", 17 | "wasi", 18 | ] 19 | 20 | [[package]] 21 | name = "libc" 22 | version = "0.2.70" 23 | source = "registry+https://github.com/rust-lang/crates.io-index" 24 | checksum = "3baa92041a6fec78c687fa0cc2b3fae8884f743d672cf551bed1d6dac6988d0f" 25 | 26 | [[package]] 27 | name = "ppv-lite86" 28 | version = "0.2.8" 29 | source = "registry+https://github.com/rust-lang/crates.io-index" 30 | checksum = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea" 31 | 32 | [[package]] 33 | name = "rand" 34 | version = "0.8.0" 35 | source = "registry+https://github.com/rust-lang/crates.io-index" 36 | checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12" 37 | dependencies = [ 38 | "libc", 39 | "rand_chacha", 40 | "rand_core", 41 | "rand_hc", 42 | ] 43 | 44 | [[package]] 45 | name = "rand_chacha" 46 | version = "0.3.0" 47 | source = "registry+https://github.com/rust-lang/crates.io-index" 48 | checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" 49 | dependencies = [ 50 | "ppv-lite86", 51 | "rand_core", 52 | ] 53 | 54 | [[package]] 55 | name = "rand_core" 56 | version = "0.6.0" 57 | source = "registry+https://github.com/rust-lang/crates.io-index" 58 | checksum = "a8b34ba8cfb21243bd8df91854c830ff0d785fff2e82ebd4434c2644cb9ada18" 59 | dependencies = [ 60 | "getrandom", 61 | ] 62 | 63 | [[package]] 64 | name = "rand_hc" 65 | version = "0.3.0" 66 | source = "registry+https://github.com/rust-lang/crates.io-index" 67 | checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" 68 | dependencies = [ 69 | "rand_core", 70 | ] 71 | 72 | [[package]] 73 | name = "rand_xorshift" 74 | version = "0.3.0" 75 | source = "registry+https://github.com/rust-lang/crates.io-index" 76 | checksum = "d25bf25ec5ae4a3f1b92f929810509a2f53d7dca2f50b794ff57e3face536c8f" 77 | dependencies = [ 78 | "rand_core", 79 | ] 80 | 81 | [[package]] 82 | name = "svg_face" 83 | version = "0.1.3" 84 | dependencies = [ 85 | "rand", 86 | "rand_xorshift", 87 | ] 88 | 89 | [[package]] 90 | name = "wasi" 91 | version = "0.9.0+wasi-snapshot-preview1" 92 | source = "registry+https://github.com/rust-lang/crates.io-index" 93 | checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" 94 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "svg_face" 3 | version = "0.1.3" 4 | authors = ["Dustin Carlino "] 5 | edition = "2018" 6 | description = "Generates random SVG faces" 7 | repository = "https://github.com/dabreegster/svg_face" 8 | categories = ["multimedia::images"] 9 | license = "Apache-2.0" 10 | readme = "README.md" 11 | 12 | [dependencies] 13 | rand = "0.8.0" 14 | 15 | [dev-dependencies] 16 | rand_xorshift = "0.3.0" 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # svg_face 2 | 3 | This is a straightforward port of https://github.com/anokhee/visual-synthesizer to Rust. 4 | 5 | ```rust 6 | use rand::SeedableRng; 7 | 8 | fn main() -> std::io::Result<()> { 9 | let mut rng = rand_xorshift::XorShiftRng::from_entropy(); 10 | let mut file = std::fs::File::create("face.svg")?; 11 | svg_face::generate_face(&mut file, &mut rng) 12 | } 13 | ``` 14 | 15 | ![face](face.svg) 16 | 17 | To try it yourself, run `git clone https://github.com/dabreegster/svg_face && cd svg_face && cargo run --example aface`. 18 | -------------------------------------------------------------------------------- /examples/aface.rs: -------------------------------------------------------------------------------- 1 | use rand::SeedableRng; 2 | 3 | fn main() -> std::io::Result<()> { 4 | let mut rng = rand_xorshift::XorShiftRng::from_entropy(); 5 | let mut file = std::fs::File::create("face.svg")?; 6 | svg_face::generate_face(&mut file, &mut rng) 7 | } 8 | -------------------------------------------------------------------------------- /face.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | use rand::Rng; 2 | use std::io::{Error, Write}; 3 | 4 | // TODO More descriptive variable names 5 | // TODO Better colors 6 | // TODO Add descriptive IDs to the results 7 | 8 | // This is a rather hacky way to avoid chopping off the left and top of some results. 9 | const OFFSET_X: f64 = 300.0; 10 | const OFFSET_Y: f64 = 300.0; 11 | 12 | /// Generates a random face and renders it as an SVG string. 13 | /// 14 | /// ``` 15 | /// use rand::SeedableRng; 16 | /// 17 | /// fn main() -> std::io::Result<()> { 18 | /// let mut rng = rand_xorshift::XorShiftRng::from_entropy(); 19 | /// let mut file = std::fs::File::create("face.svg")?; 20 | /// svg_face::generate_face(&mut file, &mut rng) 21 | /// } 22 | /// ``` 23 | pub fn generate_face(svg: &mut dyn Write, rng: &mut R) -> Result<(), Error> { 24 | let hsx = rand(rng, 145.0, 400.0); 25 | let hsy = 0.0; 26 | let hcp1x = rand(rng, 0.0, 400.0); 27 | let hcp1y = rand(rng, 190.0, 400.0); 28 | 29 | let bun_size = rand(rng, 6.0, 15.0); 30 | let buny = rand(rng, -hcp1y / 2.0, 120.0); 31 | let bunx = rand(rng, -150.0, bun_size * 2.0); 32 | 33 | let hairk = rand(rng, 5.0, 15.0); 34 | let hairstr = rand(rng, 1.5, 5.0); 35 | let hairl = rand(rng, -22.0, 33.0); 36 | let hairln = 14.0; 37 | 38 | let espac = rand(rng, 50.0 / 2.0, hsx - 30.0); 39 | let eypos = rand(rng, 0.0, hcp1y / 3.5); 40 | let ew = rand(rng, 10.0, 100.0); 41 | let eh = ew - rand(rng, 0.0, 80.0); 42 | 43 | let p = rand(rng, eh / 4.0, eh / 1.5); 44 | 45 | let ch_spacing = rand(rng, 40.0, 60.0); 46 | let ch_ypos = rand(rng, hcp1y / 4.0, hcp1y / 2.0); 47 | let ch = rand(rng, 0.0, 65.0); 48 | 49 | let mouth_x = rand(rng, 15.0, hcp1x / 3.0); 50 | let mouth_y = rand(rng, 125.0, 150.0); 51 | let mouth_cx = rand(rng, 250.0 / 6.0, 250.0 / 4.0); 52 | let mouth_cy = rand(rng, 125.0, 160.0); 53 | 54 | let nose_x = rand(rng, 10.0, 25.0); 55 | let nose_y = rand(rng, 90.0, 140.0); 56 | let nose_cx = rand(rng, 5.0, 120.0); 57 | let nose_cy = rand(rng, 0.0, 125.0); 58 | 59 | // Palette 60 | let black = (0, 0, 0); 61 | let white = (255, 255, 255); 62 | 63 | // Awesome color schemes lifted from https://github.com/anokhee/visual-synthesizer 64 | let (hair_color, skin_color, eye_color, cheeks_color) = { 65 | let (c1, c2, c3, c4) = match rng.gen_range(0..7) { 66 | 0 => ("#2D333D", "#D8BE8E", "#101B1A", "#EC4F7E"), 67 | 1 => ("#191A1A", "#E5DAC5", "#101B1A", "#B03E60"), 68 | 2 => ("#6F8120", "#A1D1BB", "#101B1A", "#8D6E61"), 69 | 3 => ("#000000", "#B8B5C8", "#A3171F", "#A49FBD"), 70 | 4 => ("#AAB656", "#58421B", "#0A2A2A", "#614B4A"), 71 | 5 => ("#6F8120", "#A0C794", "#775B5C", "#8D6E61"), 72 | 6 => ("#374B72", "#77633F", "#3A233A", "#614B4A"), 73 | _ => unreachable!(), 74 | }; 75 | (from_hex(c1), from_hex(c2), from_hex(c3), from_hex(c4)) 76 | }; 77 | 78 | // ??? 79 | let width = 800.0; 80 | let height = 600.0; 81 | 82 | writeln!( 83 | svg, 84 | r##""##, 85 | width * 2.0, 86 | height * 2.0 87 | )?; 88 | 89 | let mut style = Style { 90 | fill: None, 91 | stroke: Some(black), 92 | stroke_width: 1.0, 93 | }; 94 | 95 | { 96 | style.fill = Some(hair_color); 97 | 98 | let mut i = bun_size; 99 | while i > 0.0 { 100 | ellipse( 101 | svg, 102 | &style, 103 | width / 2.0 - hsx - bunx, 104 | height / 2.0 - buny, 105 | i * i, 106 | i * i, 107 | )?; 108 | ellipse( 109 | svg, 110 | &style, 111 | width / 2.0 + hsx + bunx, 112 | height / 2.0 - buny, 113 | i * i, 114 | i * i, 115 | )?; 116 | i -= hairstr; 117 | } 118 | } 119 | 120 | style.fill = Some(skin_color); 121 | bezier( 122 | svg, 123 | &style, 124 | width / 2.0 - hsx, 125 | height / 2.0 + hsy, 126 | width / 2.0 - hcp1x / 10.0, 127 | height / 2.0 - height / 7.0, 128 | width / 2.0 + hcp1x / 10.0, 129 | height / 2.0 - height / 7.0, 130 | width / 2.0 + hsx, 131 | height / 2.0 + hsy, 132 | )?; 133 | bezier( 134 | svg, 135 | &style, 136 | width / 2.0 - hsx, 137 | height / 2.0 + hsy, 138 | width / 2.0 - hcp1x, 139 | height / 2.0 + hcp1y, 140 | width / 2.0 + hcp1x, 141 | height / 2.0 + hcp1y, 142 | width / 2.0 + hsx, 143 | height / 2.0 + hsy, 144 | )?; 145 | 146 | style.fill = Some(cheeks_color); 147 | ellipse( 148 | svg, 149 | &style, 150 | width / 2.0 - hsx + ch_spacing, 151 | height / 2.0 + ch_ypos, 152 | ch, 153 | ch, 154 | )?; 155 | ellipse( 156 | svg, 157 | &style, 158 | width / 2.0 + hsx - ch_spacing, 159 | height / 2.0 + ch_ypos, 160 | ch, 161 | ch, 162 | )?; 163 | 164 | style.fill = Some(white); 165 | ellipse( 166 | svg, 167 | &style, 168 | width / 2.0 - espac, 169 | height / 2.0 + eypos, 170 | ew, 171 | eh, 172 | )?; 173 | ellipse( 174 | svg, 175 | &style, 176 | width / 2.0 + espac, 177 | height / 2.0 + eypos, 178 | ew, 179 | eh, 180 | )?; 181 | 182 | style.fill = Some(eye_color); 183 | ellipse(svg, &style, width / 2.0 - espac, height / 2.0 + eypos, p, p)?; 184 | ellipse(svg, &style, width / 2.0 + espac, height / 2.0 + eypos, p, p)?; 185 | 186 | // Hair 187 | { 188 | let mut i = 0.0; 189 | while i <= hairk { 190 | style.fill = if i >= hairk - 1.0 { 191 | None 192 | } else { 193 | Some(hair_color) 194 | }; 195 | bezier( 196 | svg, 197 | &style, 198 | width / 2.0 - hsx, 199 | height / 2.0 + i * hairl, 200 | width / 2.0 - hsx, 201 | height / 4.0 + i * i, 202 | width / 2.0, 203 | height / 2.5, 204 | width / 2.0, 205 | height / 2.0 - height / 8.0 + hairln, 206 | )?; 207 | bezier( 208 | svg, 209 | &style, 210 | width / 2.0 + hsx, 211 | height / 2.0 + i * hairl, 212 | width / 2.0 + hsx, 213 | height / 4.0 + i * i, 214 | width / 2.0, 215 | height / 2.5, 216 | width / 2.0, 217 | height / 2.0 - height / 8.0 + hairln, 218 | )?; 219 | i += hairstr; 220 | } 221 | } 222 | 223 | style.fill = None; 224 | style.stroke_width = 5.0; 225 | style.stroke = { 226 | let (mut r, mut g, mut b) = skin_color; 227 | if r > 45 { 228 | r -= 45; 229 | } 230 | if g > 45 { 231 | g -= 45; 232 | } 233 | if b > 45 { 234 | b -= 45; 235 | } 236 | Some((r, g, b)) 237 | }; 238 | bezier( 239 | svg, 240 | &style, 241 | width / 2.0 - mouth_x, 242 | height / 2.0 + mouth_y, 243 | width / 2.0 - mouth_cx, 244 | height / 2.0 + mouth_cy, 245 | width / 2.0 + mouth_cx, 246 | height / 2.0 + mouth_cy, 247 | width / 2.0 + mouth_x, 248 | height / 2.0 + mouth_y, 249 | )?; 250 | 251 | style.fill = Some(skin_color); 252 | bezier( 253 | svg, 254 | &style, 255 | width / 2.0 - nose_x, 256 | height / 2.0 + nose_y, 257 | width / 2.0 - nose_cx, 258 | height / 2.0 + nose_cy, 259 | width / 2.0 + nose_cx, 260 | height / 2.0 + nose_cy, 261 | width / 2.0 + nose_x, 262 | height / 2.0 + nose_y, 263 | )?; 264 | 265 | style.fill = None; 266 | style.stroke = Some(black); 267 | style.stroke_width = 1.0; 268 | bezier( 269 | svg, 270 | &style, 271 | width / 2.0 - mouth_x, 272 | height / 2.0 + mouth_y, 273 | width / 2.0 - mouth_cx, 274 | height / 2.0 + mouth_cy, 275 | width / 2.0 + mouth_cx, 276 | height / 2.0 + mouth_cy, 277 | width / 2.0 + mouth_x, 278 | height / 2.0 + mouth_y, 279 | )?; 280 | 281 | bezier( 282 | svg, 283 | &style, 284 | width / 2.0 - nose_x, 285 | height / 2.0 + nose_y, 286 | width / 2.0 - nose_cx, 287 | height / 2.0 + nose_cy, 288 | width / 2.0 + nose_cx, 289 | height / 2.0 + nose_cy, 290 | width / 2.0 + nose_x, 291 | height / 2.0 + nose_y, 292 | )?; 293 | 294 | writeln!(svg, "") 295 | } 296 | 297 | fn ellipse( 298 | svg: &mut dyn Write, 299 | style: &Style, 300 | cx: f64, 301 | cy: f64, 302 | rx: f64, 303 | ry: f64, 304 | ) -> Result<(), Error> { 305 | writeln!( 306 | svg, 307 | r##""##, 308 | cx + OFFSET_X, 309 | cy + OFFSET_Y, 310 | rx, 311 | ry, 312 | style.render() 313 | ) 314 | } 315 | 316 | fn bezier( 317 | svg: &mut dyn Write, 318 | style: &Style, 319 | x1: f64, 320 | y1: f64, 321 | x2: f64, 322 | y2: f64, 323 | x3: f64, 324 | y3: f64, 325 | x4: f64, 326 | y4: f64, 327 | ) -> Result<(), Error> { 328 | writeln!( 329 | svg, 330 | r##""##, 331 | x1 + OFFSET_X, 332 | y1 + OFFSET_Y, 333 | x2 + OFFSET_X, 334 | y2 + OFFSET_Y, 335 | x3 + OFFSET_X, 336 | y3 + OFFSET_Y, 337 | x4 + OFFSET_X, 338 | y4 + OFFSET_Y, 339 | style.render() 340 | ) 341 | } 342 | 343 | struct Style { 344 | fill: Option<(u8, u8, u8)>, 345 | stroke: Option<(u8, u8, u8)>, 346 | stroke_width: f64, 347 | } 348 | 349 | impl Style { 350 | fn render(&self) -> String { 351 | let mut inner = format!("stroke-width: {}", self.stroke_width); 352 | if let Some(rgb) = self.stroke { 353 | inner = format!("{}; stroke: rgb({}, {}, {})", inner, rgb.0, rgb.1, rgb.2); 354 | } 355 | if let Some(rgb) = self.fill { 356 | inner = format!("{}; fill: rgb({}, {}, {})", inner, rgb.0, rgb.1, rgb.2); 357 | } 358 | format!(r##"style="{}""##, inner) 359 | } 360 | } 361 | 362 | fn rand(rng: &mut R, low: f64, high: f64) -> f64 { 363 | if low <= high { 364 | rng.gen_range(low..high) 365 | } else { 366 | rng.gen_range(high..low) 367 | } 368 | } 369 | 370 | fn from_hex(raw: &str) -> (u8, u8, u8) { 371 | // Skip the leading '#' 372 | let r = u8::from_str_radix(&raw[1..3], 16).unwrap(); 373 | let g = u8::from_str_radix(&raw[3..5], 16).unwrap(); 374 | let b = u8::from_str_radix(&raw[5..7], 16).unwrap(); 375 | (r, g, b) 376 | } 377 | --------------------------------------------------------------------------------