Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 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. pub fn from(color: &PdfColor) -> Self {
  10. if let Ok(color) = color.clone().check() {
  11. pdf_color(color)
  12. } else {
  13. Self::new()
  14. }
  15. }
  16. pub fn set_from(&mut self, color: &PdfColor) {
  17. if let Ok(color) = color.clone().check() {
  18. *self = pdf_color(color);
  19. }
  20. }
  21. pub const unsafe fn from_unchecked(color: PdfColor) -> Self {
  22. Self(color)
  23. }
  24. }
  25.  
  26. #[derive(Clone)]
  27. pub enum PdfColor {
  28. Gray(f64),
  29. Spot(String, f64),
  30. Rgb(f64,f64,f64),
  31. Cmyk(f64,f64,f64,f64),
  32. }
  33.  
  34. macro_rules! warn(
  35. ($($arg:tt)*) => {
  36. println!($($arg)*);
  37. };
  38. );
  39.  
  40. impl PdfColor {
  41. pub fn check(self) -> Result<Self, ()> {
  42. use PdfColor::*;
  43. match self {
  44. Gray(g) => {
  45. if g < 0. || g > 1. {
  46. warn!("Invalid color value specified: gray={}", g);
  47. return Err(());
  48. }
  49. },
  50. Spot(_, c) => {
  51. if c < 0. || c > 1. {
  52. warn!("Invalid color value specified: grade={}", c);
  53. return Err(());
  54. }
  55. },
  56. Rgb(r,g,b) => {
  57. if r < 0. || r > 1. {
  58. warn!("Invalid color value specified: red={}", r);
  59. return Err(());
  60. }
  61. if g < 0. || g > 1. {
  62. warn!("Invalid color value specified: green={}", g);
  63. return Err(());
  64. }
  65. if b < 0. || b > 1. {
  66. warn!("Invalid color value specified: blue={}", b);
  67. return Err(());
  68. }
  69. },
  70. Cmyk(c,m,y,k) => {
  71. if c < 0. || c > 1. {
  72. warn!("Invalid color value specified: cyan={}", c);
  73. return Err(());
  74. }
  75. if m < 0. || m > 1. {
  76. warn!("Invalid color value specified: magenta={}", m);
  77. return Err(());
  78. }
  79. if y < 0. || y > 1. {
  80. warn!("Invalid color value specified: yellow={}", y);
  81. return Err(());
  82. }
  83. if k < 0. || k > 1. {
  84. warn!("Invalid color value specified: black={}", k);
  85. return Err(());
  86. }
  87. }
  88. }
  89. Ok(self)
  90. }
  91. pub fn spot(name: &str, c: f64) -> Self {
  92. PdfColor::Spot(String::from(name), c)
  93. }
  94. }
  95.  
  96. // Predefined colors
  97. const BLACK: PdfColor = PdfColor::Gray(0.);
  98. }
  99.  
  100. fn main() {
  101. use color::{pdf_color, PdfColor};
  102. let mut _a = pdf_color::new();
  103. //a = pdf_color(PdfColor::Rgb(0.5, 12.1, 0.4)); //error::private field
  104.  
  105. _a.set_from(&PdfColor::Rgb(0.5, 12.1, 0.4));
  106. unsafe {
  107. let _b = pdf_color::from_unchecked(PdfColor::Rgb(0.5, 12.1, 0.4));
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement