Browse Source

formatting

Inderjit Gill 6 months ago
parent
commit
dd4ead9925
3 changed files with 47 additions and 61 deletions
  1. 37
    38
      src/colour.rs
  2. 2
    18
      src/error.rs
  3. 8
    5
      src/lib.rs

+ 37
- 38
src/colour.rs View File

@@ -22,8 +22,7 @@
22 22
 // | LAB    | L 0..100  | A -128..128 | B -128..128 |
23 23
 // |--------+-----------+-------------+-------------|
24 24
 
25
-
26
-use crate::error::{SenError, SenResult};
25
+use crate::error::{Error, Result};
27 26
 use std;
28 27
 
29 28
 const REF_U: f64 = 0.197_830_006_642_836_807_64;
@@ -105,7 +104,7 @@ impl Colour {
105 104
         }
106 105
     }
107 106
 
108
-    pub fn clone_as(&self, format: Format) -> SenResult<Colour> {
107
+    pub fn clone_as(&self, format: Format) -> Result<Colour> {
109 108
         match *self {
110 109
             Colour::HSL(h, s, l, alpha) => match format {
111 110
                 Format::HSL => Ok(Colour::HSL(h, s, l, alpha)),
@@ -142,7 +141,7 @@ impl Colour {
142 141
                 Format::LAB => lab_from_xyz(xyz_from_rgb(*self)?),
143 142
                 Format::RGB => Ok(Colour::RGB(r, g, b, alpha)),
144 143
             },
145
-            _ => Err(SenError::IncorrectColourFormat),
144
+            _ => Err(Error::IncorrectColourFormat),
146 145
         }
147 146
     }
148 147
 }
@@ -163,7 +162,7 @@ fn axis_to_colour(a: f64) -> f64 {
163 162
     }
164 163
 }
165 164
 
166
-fn xyz_from_rgb(rgb: Colour) -> SenResult<Colour> {
165
+fn xyz_from_rgb(rgb: Colour) -> Result<Colour> {
167 166
     match rgb {
168 167
         Colour::RGB(r, g, b, alpha) => {
169 168
             let rr = colour_to_axis(r);
@@ -187,11 +186,11 @@ fn xyz_from_rgb(rgb: Colour) -> SenResult<Colour> {
187 186
 
188 187
             Ok(Colour::XYZ(x, y, z, alpha))
189 188
         }
190
-        _ => Err(SenError::IncorrectColourFormat),
189
+        _ => Err(Error::IncorrectColourFormat),
191 190
     }
192 191
 }
193 192
 
194
-fn rgb_from_xyz(xyz: Colour) -> SenResult<Colour> {
193
+fn rgb_from_xyz(xyz: Colour) -> Result<Colour> {
195 194
     match xyz {
196 195
         Colour::XYZ(x, y, z, alpha) => {
197 196
             let r = (x * 3.240_969_941_904_521_343_77)
@@ -210,7 +209,7 @@ fn rgb_from_xyz(xyz: Colour) -> SenResult<Colour> {
210 209
 
211 210
             Ok(Colour::RGB(rr, gg, bb, alpha))
212 211
         }
213
-        _ => Err(SenError::IncorrectColourFormat),
212
+        _ => Err(Error::IncorrectColourFormat),
214 213
     }
215 214
 }
216 215
 
@@ -222,7 +221,7 @@ fn axis_to_lab_component(a: f64) -> f64 {
222 221
     }
223 222
 }
224 223
 
225
-fn lab_from_xyz(xyz: Colour) -> SenResult<Colour> {
224
+fn lab_from_xyz(xyz: Colour) -> Result<Colour> {
226 225
     match xyz {
227 226
         Colour::XYZ(x, y, z, alpha) => {
228 227
             let xr = x / WHITEPOINT_0;
@@ -239,7 +238,7 @@ fn lab_from_xyz(xyz: Colour) -> SenResult<Colour> {
239 238
 
240 239
             Ok(Colour::LAB(l, a, b, alpha))
241 240
         }
242
-        _ => Err(SenError::IncorrectColourFormat),
241
+        _ => Err(Error::IncorrectColourFormat),
243 242
     }
244 243
 }
245 244
 
@@ -260,9 +259,9 @@ fn fmod(a: f64, b: f64) -> f64 {
260 259
 }
261 260
 
262 261
 // http://www.rapidtables.com/convert/color/rgb-to-hsl.htm
263
-fn hue(colour: Colour, max_chan: i32, chroma: f64) -> SenResult<f64> {
262
+fn hue(colour: Colour, max_chan: i32, chroma: f64) -> Result<f64> {
264 263
     if chroma == 0.0 {
265
-        // return Err(SenError::InvalidColourHue)
264
+        // return Err(Error::InvalidColourHue)
266 265
         return Ok(0.0);
267 266
     }
268 267
 
@@ -274,10 +273,10 @@ fn hue(colour: Colour, max_chan: i32, chroma: f64) -> SenResult<f64> {
274 273
                 0 => fmod((g - b) / chroma, 6.0),
275 274
                 1 => ((b - r) / chroma) + 2.0,
276 275
                 2 => ((r - g) / chroma) + 4.0,
277
-                _ => return Err(SenError::InvalidColourChannel),
276
+                _ => return Err(Error::InvalidColourChannel),
278 277
             }
279 278
         }
280
-        _ => return Err(SenError::IncorrectColourFormat),
279
+        _ => return Err(Error::IncorrectColourFormat),
281 280
     }
282 281
 
283 282
     angle *= 60.0;
@@ -290,7 +289,7 @@ fn hue(colour: Colour, max_chan: i32, chroma: f64) -> SenResult<f64> {
290 289
 }
291 290
 
292 291
 // http://www.rapidtables.com/convert/color/rgb-to-hsl.htm
293
-fn hsl_from_rgb(colour: Colour) -> SenResult<Colour> {
292
+fn hsl_from_rgb(colour: Colour) -> Result<Colour> {
294 293
     match colour {
295 294
         Colour::RGB(r, g, b, alpha) => {
296 295
             let min_val = r.min(g).min(b);
@@ -309,11 +308,11 @@ fn hsl_from_rgb(colour: Colour) -> SenResult<Colour> {
309 308
 
310 309
             Ok(Colour::HSL(h, saturation, lightness, alpha))
311 310
         }
312
-        _ => Err(SenError::IncorrectColourFormat),
311
+        _ => Err(Error::IncorrectColourFormat),
313 312
     }
314 313
 }
315 314
 
316
-fn hsv_from_rgb(colour: Colour) -> SenResult<Colour> {
315
+fn hsv_from_rgb(colour: Colour) -> Result<Colour> {
317 316
     match colour {
318 317
         Colour::RGB(r, g, b, alpha) => {
319 318
             let min_val = r.min(g).min(b);
@@ -332,7 +331,7 @@ fn hsv_from_rgb(colour: Colour) -> SenResult<Colour> {
332 331
 
333 332
             Ok(Colour::HSV(h, saturation, max_val, alpha))
334 333
         }
335
-        _ => Err(SenError::IncorrectColourFormat),
334
+        _ => Err(Error::IncorrectColourFormat),
336 335
     }
337 336
 }
338 337
 
@@ -378,7 +377,7 @@ fn rgb_from_chm(chroma: f64, h: f64, m: f64, alpha: f64) -> Colour {
378 377
     Colour::RGB(r + m, g + m, b + m, alpha)
379 378
 }
380 379
 
381
-fn rgb_from_hsl(hsl: Colour) -> SenResult<Colour> {
380
+fn rgb_from_hsl(hsl: Colour) -> Result<Colour> {
382 381
     match hsl {
383 382
         Colour::HSL(h, s, l, alpha) => {
384 383
             let chroma = (1.0 - ((2.0 * l) - 1.0).abs()) * s;
@@ -389,7 +388,7 @@ fn rgb_from_hsl(hsl: Colour) -> SenResult<Colour> {
389 388
 
390 389
             Ok(rgb_from_chm(chroma, h, m, alpha))
391 390
         }
392
-        _ => Err(SenError::IncorrectColourFormat),
391
+        _ => Err(Error::IncorrectColourFormat),
393 392
     }
394 393
 }
395 394
 
@@ -401,7 +400,7 @@ fn lab_component_to_axis(l: f64) -> f64 {
401 400
     }
402 401
 }
403 402
 
404
-fn xyz_from_lab(lab: Colour) -> SenResult<Colour> {
403
+fn xyz_from_lab(lab: Colour) -> Result<Colour> {
405 404
     match lab {
406 405
         Colour::LAB(l, a, b, alpha) => {
407 406
             let fy = (l + 16.0) / 116.0;
@@ -425,11 +424,11 @@ fn xyz_from_lab(lab: Colour) -> SenResult<Colour> {
425 424
                 alpha,
426 425
             ))
427 426
         }
428
-        _ => Err(SenError::IncorrectColourFormat),
427
+        _ => Err(Error::IncorrectColourFormat),
429 428
     }
430 429
 }
431 430
 
432
-fn rgb_from_hsv(hsv: Colour) -> SenResult<Colour> {
431
+fn rgb_from_hsv(hsv: Colour) -> Result<Colour> {
433 432
     match hsv {
434 433
         Colour::HSV(h, s, v, alpha) => {
435 434
             let chroma = v * s;
@@ -437,7 +436,7 @@ fn rgb_from_hsv(hsv: Colour) -> SenResult<Colour> {
437 436
 
438 437
             Ok(rgb_from_chm(chroma, h, m, alpha))
439 438
         }
440
-        _ => Err(SenError::IncorrectColourFormat),
439
+        _ => Err(Error::IncorrectColourFormat),
441 440
     }
442 441
 }
443 442
 
@@ -556,7 +555,7 @@ fn l2y(l: f64) -> f64 {
556 555
     }
557 556
 }
558 557
 
559
-fn luv_from_xyz(xyz: Colour) -> SenResult<Colour> {
558
+fn luv_from_xyz(xyz: Colour) -> Result<Colour> {
560 559
     match xyz {
561 560
         Colour::XYZ(x, y, z, alpha) => {
562 561
             let var_u = (4.0 * x) / (x + (15.0 * y) + (3.0 * z));
@@ -571,11 +570,11 @@ fn luv_from_xyz(xyz: Colour) -> SenResult<Colour> {
571 570
                 Ok(Colour::LUV(l, u, v, alpha))
572 571
             }
573 572
         }
574
-        _ => Err(SenError::IncorrectColourFormat),
573
+        _ => Err(Error::IncorrectColourFormat),
575 574
     }
576 575
 }
577 576
 
578
-fn xyz_from_luv(luv: Colour) -> SenResult<Colour> {
577
+fn xyz_from_luv(luv: Colour) -> Result<Colour> {
579 578
     match luv {
580 579
         Colour::LUV(l, u, v, alpha) => {
581 580
             if l <= 0.000_000_01 {
@@ -590,11 +589,11 @@ fn xyz_from_luv(luv: Colour) -> SenResult<Colour> {
590 589
 
591 590
             Ok(Colour::XYZ(x, y, z, alpha))
592 591
         }
593
-        _ => Err(SenError::IncorrectColourFormat),
592
+        _ => Err(Error::IncorrectColourFormat),
594 593
     }
595 594
 }
596 595
 
597
-fn lch_from_luv(luv: Colour) -> SenResult<Colour> {
596
+fn lch_from_luv(luv: Colour) -> Result<Colour> {
598 597
     match luv {
599 598
         Colour::LUV(l, u, v, alpha) => {
600 599
             let mut h: f64;
@@ -611,11 +610,11 @@ fn lch_from_luv(luv: Colour) -> SenResult<Colour> {
611 610
 
612 611
             Ok(Colour::LCH(l, c, h, alpha))
613 612
         }
614
-        _ => Err(SenError::IncorrectColourFormat),
613
+        _ => Err(Error::IncorrectColourFormat),
615 614
     }
616 615
 }
617 616
 
618
-fn luv_from_lch(lch: Colour) -> SenResult<Colour> {
617
+fn luv_from_lch(lch: Colour) -> Result<Colour> {
619 618
     match lch {
620 619
         Colour::LCH(l, c, h, alpha) => {
621 620
             let hrad = h * 0.017_453_292_519_943_295_77; /* (pi / 180.0) */
@@ -624,11 +623,11 @@ fn luv_from_lch(lch: Colour) -> SenResult<Colour> {
624 623
 
625 624
             Ok(Colour::LUV(l, u, v, alpha))
626 625
         }
627
-        _ => Err(SenError::IncorrectColourFormat),
626
+        _ => Err(Error::IncorrectColourFormat),
628 627
     }
629 628
 }
630 629
 
631
-fn lch_from_hsluv(hsluv: Colour) -> SenResult<Colour> {
630
+fn lch_from_hsluv(hsluv: Colour) -> Result<Colour> {
632 631
     match hsluv {
633 632
         Colour::HSLuv(h, s, l, alpha) => {
634 633
             let c = if l > 99.999_999_9 || l < 0.000_000_01 {
@@ -643,11 +642,11 @@ fn lch_from_hsluv(hsluv: Colour) -> SenResult<Colour> {
643 642
                 Ok(Colour::LCH(l, c, h, alpha))
644 643
             }
645 644
         }
646
-        _ => Err(SenError::IncorrectColourFormat),
645
+        _ => Err(Error::IncorrectColourFormat),
647 646
     }
648 647
 }
649 648
 
650
-fn hsluv_from_lch(lch: Colour) -> SenResult<Colour> {
649
+fn hsluv_from_lch(lch: Colour) -> Result<Colour> {
651 650
     match lch {
652 651
         Colour::LCH(l, c, h, alpha) => {
653 652
             let s = if l > 99.999_999_9 || l < 0.000_000_01 {
@@ -662,15 +661,15 @@ fn hsluv_from_lch(lch: Colour) -> SenResult<Colour> {
662 661
                 Ok(Colour::HSLuv(h, s, l, alpha))
663 662
             }
664 663
         }
665
-        _ => Err(SenError::IncorrectColourFormat),
664
+        _ => Err(Error::IncorrectColourFormat),
666 665
     }
667 666
 }
668 667
 
669
-fn xyz_from_hsluv(hsluv: Colour) -> SenResult<Colour> {
668
+fn xyz_from_hsluv(hsluv: Colour) -> Result<Colour> {
670 669
     xyz_from_luv(luv_from_lch(lch_from_hsluv(hsluv)?)?)
671 670
 }
672 671
 
673
-fn hsluv_from_xyz(xyz: Colour) -> SenResult<Colour> {
672
+fn hsluv_from_xyz(xyz: Colour) -> Result<Colour> {
674 673
     hsluv_from_lch(lch_from_luv(luv_from_xyz(xyz)?)?)
675 674
 }
676 675
 

+ 2
- 18
src/error.rs View File

@@ -13,27 +13,11 @@
13 13
 // You should have received a copy of the GNU General Public License
14 14
 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 15
 
16
-pub type SenResult<T> = ::std::result::Result<T, SenError>;
16
+pub type Result<T> = ::std::result::Result<T, Error>;
17 17
 
18 18
 #[derive(Debug)]
19
-pub enum SenError {
20
-    GeneralError,
21
-
22
-    // colour
23
-    //
19
+pub enum Error {
24 20
     IncorrectColourFormat,
25 21
     InvalidColourHue,
26 22
     InvalidColourChannel,
27
-
28
-    // parser
29
-    ParserInvalidChar(char),
30
-    ParserInvalidLiteral,
31
-    ParserUnableToParseFloat(String),
32
-    ParserHandledToken,
33
-
34
-
35
-    // MemorySegmentType
36
-    MemorySegmentTypeUnmappableBytecodeArg,
37
-    MemorySegmentTypeUnmappableI32,
38
-
39 23
 }

+ 8
- 5
src/lib.rs View File

@@ -13,18 +13,21 @@
13 13
 // You should have received a copy of the GNU General Public License
14 14
 // along with this program.  If not, see <https://www.gnu.org/licenses/>.
15 15
 #![allow(dead_code)]
16
-#![cfg_attr(feature = "cargo-clippy", allow(many_single_char_names, too_many_arguments))]
16
+#![cfg_attr(
17
+    feature = "cargo-clippy",
18
+    allow(many_single_char_names, too_many_arguments)
19
+)]
17 20
 
18 21
 extern crate cfg_if;
19 22
 extern crate wasm_bindgen;
20 23
 
24
+mod colour;
21 25
 mod error;
22 26
 mod utils;
23
-mod colour;
24 27
 
28
+use crate::colour::{Colour, Format};
25 29
 use cfg_if::cfg_if;
26 30
 use wasm_bindgen::prelude::*;
27
-use crate::colour::{Colour, Format};
28 31
 
29 32
 cfg_if! {
30 33
     // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
@@ -86,7 +89,7 @@ pub fn hsl_from_rgb(r: f64, g: f64, b: f64) -> Transport3C {
86 89
 
87 90
     match hsluv {
88 91
         Colour::HSLuv(h, s, l, _) => Transport3C::new(h, s, l),
89
-        _ => Transport3C::blank()
92
+        _ => Transport3C::blank(),
90 93
     }
91 94
 }
92 95
 
@@ -96,6 +99,6 @@ pub fn rgb_from_hsl(h: f64, s: f64, l: f64) -> Transport3C {
96 99
 
97 100
     match rgb {
98 101
         Colour::RGB(r, g, b, _) => Transport3C::new(r, g, b),
99
-        _ => Transport3C::blank()
102
+        _ => Transport3C::blank(),
100 103
     }
101 104
 }

Loading…
Cancel
Save