Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. mod color {
  2. #[allow(non_camel_case_types)]
  3. pub struct pdf_color(PdfColor);
  4.  
  5. impl pdf_color {
  6. pub fn new() -> Self {
  7. Self(BLACK)
  8. }
  9. }
  10.  
  11. #[derive(Clone)]
  12. pub enum PdfColor {
  13. Gray(f64),
  14. Spot(String, f64),
  15. Rgb(f64,f64,f64),
  16. Cmyk(f64,f64,f64,f64),
  17. }
  18.  
  19. macro_rules! warn(
  20. ($($arg:tt)*) => {
  21. println!($($arg)*);
  22. };
  23. );
  24.  
  25. impl PdfColor {
  26. pub fn warn(self) -> pdf_color {
  27. use PdfColor::*;
  28. match self {
  29. Gray(g) => {
  30. if g < 0. || g > 1. {
  31. warn!("Invalid color value specified: gray={}", g);
  32. }
  33. },
  34. Spot(_, c) => {
  35. if c < 0. || c > 1. {
  36. warn!("Invalid color value specified: grade={}", c);
  37. }
  38. },
  39. Rgb(r,g,b) => {
  40. if r < 0. || r > 1. {
  41. warn!("Invalid color value specified: red={}", r);
  42. }
  43. if g < 0. || g > 1. {
  44. warn!("Invalid color value specified: green={}", g);
  45. }
  46. if b < 0. || b > 1. {
  47. warn!("Invalid color value specified: blue={}", b);
  48. }
  49. },
  50. Cmyk(c,m,y,k) => {
  51. if c < 0. || c > 1. {
  52. warn!("Invalid color value specified: cyan={}", c);
  53. }
  54. if m < 0. || m > 1. {
  55. warn!("Invalid color value specified: magenta={}", m);
  56. }
  57. if y < 0. || y > 1. {
  58. warn!("Invalid color value specified: yellow={}", y);
  59. }
  60. if k < 0. || k > 1. {
  61. warn!("Invalid color value specified: black={}", k);
  62. }
  63. }
  64. }
  65. pdf_color(self)
  66. }
  67. pub fn spot(name: &str, c: f64) -> Self {
  68. PdfColor::Spot(String::from(name), c)
  69. }
  70. }
  71.  
  72. // Predefined colors
  73. const BLACK: PdfColor = PdfColor::Gray(0.);
  74. }
  75.  
  76. fn main() {
  77. use color::{pdf_color, PdfColor};
  78. let mut _a = pdf_color::new();
  79. //a = pdf_color(PdfColor::Rgb(0.5, 12.1, 0.4)); //error::private field
  80. _a = PdfColor::Rgb(0.5, 12.1, 0.4).warn();
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement