@@ -17,10 +17,14 @@ pub type ARGBInts = (
1717/// color formats. The gnuplot [colorspec reference](http://gnuplot.info/docs_6.0/loc3640.html)
1818/// also explains these.
1919///
20+ /// **NOTE**: Gnuplot interprets the alpha channel in an unusual way, where 0 is fully opaque and
21+ /// 255 is fully transparent. This wrapper inverts the meaning (as described below) to match the
22+ /// more common interpretation.
23+ ///
2024/// There are many equivalent ways of specifying colors, and this allows the user to chose the most convenient.
2125/// For example, all the following will produce the same blue color:
22- /// `RGBColor("blue".into())`, `RGBColor("0x0000ff".into())`, `RGBColor("#0000ff".into())`, `RGBColor("0x000000ff ".into())`,
23- /// `RGBColor("#000000ff ".into())`, `RGBIntegerColor(0, 0, 255)`, `ARGBColor(0 , 0, 0, 255)`,
26+ /// `RGBColor("blue".into())`, `RGBColor("0x0000ff".into())`, `RGBColor("#0000ff".into())`, `RGBColor("0xff0000ff ".into())`,
27+ /// `RGBColor("#ff0000ff ".into())`, `RGBIntegerColor(0, 0, 255)`, `ARGBColor(255 , 0, 0, 255)`,
2428///
2529/// See example usages of these colors in `color.rs` and `variable_color.rs` in the
2630/// [Examples folder](https://github.com/SiegeLord/RustGnuplot/tree/master/gnuplot/examples) on Github
@@ -35,15 +39,15 @@ pub enum ColorType<T = String>
3539 /// - #RRGGBB --- string containing hexadecimal in x11 format
3640 /// - #AARRGGBB --- string containing hexadecimal in x11 format
3741 ///
38- /// "#AARRGGBB" represents an RGB color with an alpha channel (transparency) value in the high bits.
39- /// An alpha value of 0 represents a fully opaque color; i.e., "#00RRGGBB " is the same as "#RRGGBB".
40- /// An alpha value of 255 (FF) represents full transparency.
42+ /// "#AARRGGBB" represents an RGB color with an alpha channel value in the high bits.
43+ /// An alpha value of 255 (FF) represents a fully opaque color; i.e., "#FFRRGGBB " is the same as "#RRGGBB".
44+ /// An alpha value of 0 represents full transparency.
4145 RGBString ( T ) ,
4246 /// tuple of u8 representing red, green and blue values as 0-255
4347 RGBInteger ( ColorComponent , ColorComponent , ColorComponent ) ,
4448 /// tuple of u8 representing alpha, red, green and blue values as 0-255.
45- /// As with `RGBColor`, an alpha value of 0 represents a fully opaque color;
46- /// an alpha value of 255 (FF) represents full transparency.
49+ /// As with `RGBColor`, an alpha value of 255 (FF) represents a fully opaque color;
50+ /// an alpha value of 0 represents full transparency.
4751 ARGBInteger (
4852 ColorComponent ,
4953 ColorComponent ,
@@ -102,7 +106,7 @@ pub enum ColorType<T = String>
102106 VariablePaletteColor ( Vec < f64 > ) ,
103107 /// Similar to `VariablePaletteColor` in that it takes a `Vec<f64>` to set the indexes into the
104108 /// color map for each data point, but in addition to the color data it takes a string hold the name
105- /// of the color map to use. This should have been previously created in the workspace using the
109+ /// of the colormap to use. This should have been previously created in the workspace using the
106110 /// [create_colormap()](crate::AxesCommon::create_colormap) function.
107111 SavedColorMap ( T , Vec < f64 > ) ,
108112 /// Set the color of all elements of the plot to the `n`th color in the current gnuplot color cycle.
@@ -127,8 +131,8 @@ impl<T: Display + Debug> ColorType<T>
127131 {
128132 match self
129133 {
130- RGBString ( s) => format ! ( r#"rgb "{}""# , s ) ,
131- RGBInteger ( r, g, b) => format ! ( r#"rgb {}"# , from_argb( 0 , * r, * g, * b) ) ,
134+ RGBString ( s) => format ! ( r#"rgb "{}""# , from_string ( s . to_string ( ) ) ) ,
135+ RGBInteger ( r, g, b) => format ! ( r#"rgb {}"# , from_argb( 255 , * r, * g, * b) ) ,
132136 ARGBInteger ( a, r, g, b) => format ! ( r#"rgb {}"# , from_argb( * a, * r, * g, * b) ) ,
133137 VariableRGBInteger ( _) => "rgb variable" . into ( ) ,
134138 VariableARGBInteger ( _) => "rgb variable" . into ( ) ,
@@ -149,7 +153,7 @@ impl<T: Display + Debug> ColorType<T>
149153 {
150154 VariableRGBInteger ( items) => items
151155 . iter ( )
152- . map ( |( r, g, b) | from_argb ( 0 , * r, * g, * b) as f64 )
156+ . map ( |( r, g, b) | from_argb ( 255 , * r, * g, * b) as f64 )
153157 . collect ( ) ,
154158 VariableARGBInteger ( items) => items
155159 . iter ( )
@@ -192,7 +196,39 @@ impl<T: Display + Debug> ColorType<T>
192196fn from_argb ( a : ColorComponent , r : ColorComponent , g : ColorComponent , b : ColorComponent )
193197 -> ColorInt
194198{
195- ( ( a as ColorInt ) << 24 ) + ( ( r as ColorInt ) << 16 ) + ( ( g as ColorInt ) << 8 ) + ( b as ColorInt )
199+ ( ( 255 - a as ColorInt ) << 24 )
200+ + ( ( r as ColorInt ) << 16 )
201+ + ( ( g as ColorInt ) << 8 )
202+ + ( b as ColorInt )
203+ }
204+
205+ fn from_string ( argb : String ) -> String
206+ {
207+ if let Some ( trimmed_argb) = argb. strip_prefix ( "0x" ) . or_else ( || argb. strip_prefix ( "#" ) )
208+ {
209+ if trimmed_argb. len ( ) == 8
210+ {
211+ if let Ok ( argb_int) = ColorInt :: from_str_radix ( trimmed_argb, 16 )
212+ {
213+ let a = 255 - ( ( argb_int >> 24 ) & 0xff ) ;
214+ let argb_int = ( a << 24 ) + ( argb_int & 0xffffff ) ;
215+ format ! ( "#{:08x}" , argb_int)
216+ }
217+ else
218+ {
219+ // Let gnuplot sort it out.
220+ argb
221+ }
222+ }
223+ else
224+ {
225+ argb
226+ }
227+ }
228+ else
229+ {
230+ argb
231+ }
196232}
197233
198234fn float_color_to_int ( v : f64 ) -> Result < u8 , String >
0 commit comments