|
| 1 | +use std::{fmt::Debug, iter}; |
| 2 | + |
| 3 | +// This file is released into Public Domain. |
| 4 | +use crate::common::*; |
| 5 | +use gnuplot::*; |
| 6 | + |
| 7 | +mod common; |
| 8 | + |
| 9 | +fn color_name<T: Debug>(color: &PlotOption<T>) -> String |
| 10 | +{ |
| 11 | + match color |
| 12 | + { |
| 13 | + Color(color_type) => format!("{:?}", color_type), |
| 14 | + _ => panic!(), |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +fn example(c: Common) |
| 19 | +{ |
| 20 | + let x = 0..5; |
| 21 | + |
| 22 | + let colors = [ |
| 23 | + Color("black".into()), // Conversion to RGBString is implicit |
| 24 | + Color(ColorType::RGBString("black")), // Explicit use of RGBString |
| 25 | + Color("red".into()), // Conversion to RGBString is implicit |
| 26 | + Color(RGBString("#ff0000")), // red using Hex coded RRGGBB |
| 27 | + Color(RGBString("#00ff0000")), // red using Hex coded AARRGGBB |
| 28 | + Color("#ff8888".into()), // pink using Hex coded RRGGBB. Conversion to RGBString is implict |
| 29 | + Color("#88ff0000".into()), // pink using Hex coded AARRGGBB. Conversion to RGBString is implict |
| 30 | + Color(ColorType::RGBString("#ffff0000")), // transparent using Hex coded AARRGGBB |
| 31 | + Color((128, 0, 255).into()), // purple using implict RGBInteger |
| 32 | + Color(RGBInteger(128, 0, 255)), // purple using explict RGBInteger |
| 33 | + Color((0.5, 0.0, 1.0).try_into().unwrap()), // purple using implict float to int conversion |
| 34 | + Color((128, 128, 0, 255).into()), // pale purple using implict ARGBInteger |
| 35 | + Color(ARGBInteger(128, 128, 0, 255)), // pale purple using explict ARGBInteger |
| 36 | + Color((0.5, 0.5, 0.0, 1.0).try_into().unwrap()), // pale purple using implict float to int conversion |
| 37 | + ]; |
| 38 | + |
| 39 | + let mut fg = Figure::new(); |
| 40 | + let ax = fg.axes2d(); |
| 41 | + ax.set_title( |
| 42 | + "Demo of RGBString in various forms\nSee code comments for how to construct the colors", |
| 43 | + &[], |
| 44 | + ) |
| 45 | + .set_x_range(Fix(-9.0), Auto) |
| 46 | + .set_legend(Graph(0.5), Graph(0.9), &[], &[Font("", 14.0)]); |
| 47 | + |
| 48 | + let n_colors = colors.len(); |
| 49 | + for (i, color) in colors.into_iter().enumerate() |
| 50 | + { |
| 51 | + ax.box_xy_error_delta( |
| 52 | + x.clone(), |
| 53 | + iter::repeat((n_colors - 1) - i), |
| 54 | + iter::repeat(0.4), |
| 55 | + iter::repeat(0.2), |
| 56 | + &[ |
| 57 | + Caption(&color_name(&color)), |
| 58 | + LineWidth(1.0), |
| 59 | + BorderColor("black".into()), |
| 60 | + color, |
| 61 | + ], |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + // Draw line across the boxes in fixed black and background colors |
| 66 | + ax.lines( |
| 67 | + [0, 0], |
| 68 | + [0, n_colors - 1], |
| 69 | + &[ |
| 70 | + LineWidth(7.0), |
| 71 | + Color(Black), |
| 72 | + Caption(&color_name::<String>(&Color(Black))), |
| 73 | + ], |
| 74 | + ); |
| 75 | + |
| 76 | + ax.lines( |
| 77 | + [4, 4], |
| 78 | + [0, n_colors - 1], |
| 79 | + &[ |
| 80 | + LineWidth(7.0), |
| 81 | + Color(Background), |
| 82 | + Caption(&color_name::<String>(&Color(Background))), |
| 83 | + ], |
| 84 | + ); |
| 85 | + |
| 86 | + // any of the forms used for Color can also be used with TextColor and BorderColor |
| 87 | + ax.set_x_label( |
| 88 | + "Labels can be colored using the TextColor function", |
| 89 | + &[TextColor((128, 0, 255).into())], |
| 90 | + ); |
| 91 | + |
| 92 | + c.show(&mut fg, "rgb_color"); |
| 93 | + |
| 94 | + // ######################################################################## |
| 95 | + |
| 96 | + let mut fg = Figure::new(); |
| 97 | + let ax = fg.axes2d(); |
| 98 | + let max_cb = 10.0; |
| 99 | + ax.set_cb_range(Fix(0.0), Fix(max_cb)); |
| 100 | + for color_value in 0..=10 |
| 101 | + { |
| 102 | + let color_float = color_value as f64; |
| 103 | + let frac_color = Color(PaletteFracColor(color_float / max_cb)); |
| 104 | + let cb_range_color = Color(PaletteCBColor(color_float)); |
| 105 | + |
| 106 | + ax.box_xy_error_delta( |
| 107 | + [color_value], |
| 108 | + [0], |
| 109 | + [0.4], |
| 110 | + [0.4], |
| 111 | + &[ |
| 112 | + Caption(&color_name(&frac_color)), |
| 113 | + LineWidth(1.0), |
| 114 | + BorderColor("black".into()), |
| 115 | + frac_color, |
| 116 | + ], |
| 117 | + ) |
| 118 | + .box_xy_error_delta( |
| 119 | + [color_value], |
| 120 | + [1], |
| 121 | + [0.4], |
| 122 | + [0.4], |
| 123 | + &[ |
| 124 | + Caption(&color_name(&cb_range_color)), |
| 125 | + LineWidth(1.0), |
| 126 | + BorderColor("black".into()), |
| 127 | + cb_range_color, |
| 128 | + ], |
| 129 | + ); |
| 130 | + } |
| 131 | + ax.set_x_range(Fix(-10.0), Fix(11.0)) |
| 132 | + .set_y_range(Fix(-0.5), Fix(1.5)) |
| 133 | + .set_legend(Graph(0.45), Graph(0.9), &[], &[Font("", 14.0)]); |
| 134 | + c.show(&mut fg, "palette_colors"); |
| 135 | +} |
| 136 | + |
| 137 | +fn main() |
| 138 | +{ |
| 139 | + Common::new().map(|c| example(c)); |
| 140 | +} |
0 commit comments