Browse Source

(Color)foo -> new Color (foo)

Tig Kindel 1 year ago
parent
commit
5ffaf55efc

+ 2 - 2
Terminal.Gui/Drawing/Color.cs

@@ -283,6 +283,7 @@ public class Color : IEquatable<Color> {
 
 	/// <summary>
 	/// Gets or sets the <see cref="Color"/> using a legacy 16-color <see cref="Gui.ColorName"/> value.
+	/// <see langword="get"/> will return the closest 16 color match to the true color when no exact value is found.
 	/// </summary>
 	/// <remarks>
 	/// Get returns the <see cref="ColorName"/> of the closest 24-bit color value. Set sets the RGB value using a hard-coded map.
@@ -301,7 +302,6 @@ public class Color : IEquatable<Color> {
 
 	#region Legacy Color Names
 	/// <summary>
-	/// 
 	/// The black color.
 	/// </summary>
 	public const ColorName Black = ColorName.Black;
@@ -694,7 +694,7 @@ public readonly struct Attribute : IEquatable<Attribute> {
 	/// <param name="platformColor">platform-dependent color value.</param>
 	/// <param name="foreground">Foreground</param>
 	/// <param name="background">Background</param>
-	internal Attribute (int platformColor, ColorName foreground, ColorName background) : this (platformColor, (Color)foreground, (Color)background) { }
+	internal Attribute (int platformColor, ColorName foreground, ColorName background) : this (platformColor, new Color (foreground), new Color (background)) { }
 
 	/// <summary>
 	/// Initializes a new instance of the <see cref="Attribute"/> struct.

+ 4 - 4
Terminal.Gui/Views/FileDialog.cs

@@ -177,7 +177,7 @@ namespace Terminal.Gui {
 
 			this.tbPath = new TextField {
 				Width = Dim.Fill (0),
-				CaptionColor = (Color)Color.Black
+				CaptionColor = new Color (Color.Black)
 			};
 			this.tbPath.KeyPress += (s, k) => {
 
@@ -273,7 +273,7 @@ namespace Terminal.Gui {
 
 			tbFind = new TextField {
 				X = Pos.Right (this.btnToggleSplitterCollapse) + 1,
-				CaptionColor = (Color)Color.Black,
+				CaptionColor = new Color (Color.Black),
 				Width = 30,
 				Y = Pos.AnchorEnd (1),
 			};
@@ -1197,8 +1197,8 @@ namespace Terminal.Gui {
 			}
 
 
-			var color = Style.ColorProvider.GetColor (stats.FileSystemInfo) ?? (Color)Color.White;
-			var black = (Color)Color.Black;
+			var color = Style.ColorProvider.GetColor (stats.FileSystemInfo) ?? new Color (Color.White);
+			var black = new Color (Color.Black);
 
 			// TODO: Add some kind of cache for this
 			return new ColorScheme {

+ 1 - 1
Terminal.Gui/Views/TextField.cs

@@ -40,7 +40,7 @@ namespace Terminal.Gui {
 		/// Gets or sets the foreground <see cref="Color"/> to use when 
 		/// rendering <see cref="Caption"/>.
 		/// </summary>
-		public Color CaptionColor { get; set; } = (Color)Color.DarkGray;
+		public Color CaptionColor { get; set; } = new Color (Color.DarkGray);
 
 		/// <summary>
 		/// Tracks whether the text field should be considered "used", that is, that the user has moved in the entry, so new input should be appended at the cursor position, rather than clearing the entry

+ 1 - 1
Terminal.Gui/Views/TextValidateField.cs

@@ -509,7 +509,7 @@ namespace Terminal.Gui {
 				return;
 			}
 
-			var bgcolor = !IsValid ? (Color)Color.BrightRed : ColorScheme.Focus.Background;
+			var bgcolor = !IsValid ? new Color (Color.BrightRed) : ColorScheme.Focus.Background;
 			var textColor = new Attribute (ColorScheme.Focus.Foreground, bgcolor);
 
 			var (margin_left, margin_right) = GetMargins (Bounds.Width);

+ 1 - 1
UICatalog/Scenarios/ColorPicker.cs

@@ -117,7 +117,7 @@ namespace UICatalog.Scenarios {
 		private void UpdateColorLabel (Label label, ColorPicker colorPicker)
 		{
 			label.Clear ();
-			var color = (Color)colorPicker.SelectedColor;
+			var color = new Color (colorPicker.SelectedColor);
 			label.Text = $"{colorPicker.SelectedColor} ({(int)colorPicker.SelectedColor}) #{color.R:X2}{color.G:X2}{color.B:X2}";
 		}
 

+ 1 - 1
UICatalog/Scenarios/LineDrawing.cs

@@ -95,7 +95,7 @@ namespace UICatalog.Scenarios {
 		class DrawingArea : View {
 			List<LineCanvas> _layers = new List<LineCanvas> ();
 			LineCanvas _currentLayer;
-			Color _currentColor = (Color)Color.White;
+			Color _currentColor = new Color (Color.White);
 			StraightLine _currentLine = null;
 
 			public LineStyle LineStyle { get; set; }

+ 8 - 8
UnitTests/Configuration/ConfigurationMangerTests.cs

@@ -325,8 +325,8 @@ namespace Terminal.Gui.ConfigurationTests {
 
 			Assert.Equal (Key.Q | Key.CtrlMask, Application.QuitKey);
 
-			Assert.Equal ((Color)Color.White, Colors.ColorSchemes ["Base"].Normal.Foreground);
-			Assert.Equal ((Color)Color.Blue, Colors.ColorSchemes ["Base"].Normal.Background);
+			Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
+			Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
 
 			// Change Base
 			var json = ConfigurationManager.ToStream ();
@@ -509,12 +509,12 @@ namespace Terminal.Gui.ConfigurationTests {
 
 			Assert.Equal ("Default", ConfigurationManager.Themes.Theme);
 
-			Assert.Equal ((Color)Color.White, Colors.ColorSchemes ["Base"].Normal.Foreground);
-			Assert.Equal ((Color)Color.Blue, Colors.ColorSchemes ["Base"].Normal.Background);
+			Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
+			Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
 
 			var colorSchemes = (Dictionary<string, ColorScheme>)Themes.First ().Value ["ColorSchemes"].PropertyValue;
-			Assert.Equal ((Color)Color.White, colorSchemes ["Base"].Normal.Foreground);
-			Assert.Equal ((Color)Color.Blue, colorSchemes ["Base"].Normal.Background);
+			Assert.Equal (new Color (Color.White), colorSchemes ["Base"].Normal.Foreground);
+			Assert.Equal (new Color (Color.Blue), colorSchemes ["Base"].Normal.Background);
 
 			// Now re-apply
 			ConfigurationManager.Apply ();
@@ -522,8 +522,8 @@ namespace Terminal.Gui.ConfigurationTests {
 			Assert.Equal (Key.Z | Key.AltMask, Application.QuitKey);
 			Assert.Equal ("Default", ConfigurationManager.Themes.Theme);
 
-			Assert.Equal ((Color)Color.White, Colors.ColorSchemes ["Base"].Normal.Foreground);
-			Assert.Equal ((Color)Color.Blue, Colors.ColorSchemes ["Base"].Normal.Background);
+			Assert.Equal (new Color (Color.White), Colors.ColorSchemes ["Base"].Normal.Foreground);
+			Assert.Equal (new Color (Color.Blue), Colors.ColorSchemes ["Base"].Normal.Background);
 		}
 
 		[Fact, AutoInitShutdown]

+ 4 - 4
UnitTests/Configuration/JsonConverterTests.cs

@@ -30,7 +30,7 @@ namespace Terminal.Gui.ConfigurationTests {
 			Color actualColor = JsonSerializer.Deserialize<Color> (json, ConfigurationManagerTests._jsonOptions);
 
 			// Assert
-			Assert.Equal ((Color)expectedColor, actualColor);
+			Assert.Equal (new Color (expectedColor), actualColor);
 		}
 
 		[Theory]
@@ -55,7 +55,7 @@ namespace Terminal.Gui.ConfigurationTests {
 			var converter = new ColorJsonConverter ();
 			var options = new JsonSerializerOptions { Converters = { converter } };
 
-			var serialized = JsonSerializer.Serialize<Color> ((Color)colorName, options);
+			var serialized = JsonSerializer.Serialize<Color> (new Color (colorName), options);
 
 			Assert.Equal ($"\"{expectedJson}\"", serialized);
 		}
@@ -67,7 +67,7 @@ namespace Terminal.Gui.ConfigurationTests {
 			var expectedJson = "\"Black\"";
 
 			// Act
-			var json = JsonSerializer.Serialize<Color> ((Color)Color.Black, new JsonSerializerOptions {
+			var json = JsonSerializer.Serialize<Color> (new Color (Color.Black), new JsonSerializerOptions {
 				Converters = { new ColorJsonConverter () }
 			});
 
@@ -82,7 +82,7 @@ namespace Terminal.Gui.ConfigurationTests {
 			var expectedJson = "\"BrightRed\"";
 
 			// Act
-			var json = JsonSerializer.Serialize<Color> ((Color)Color.BrightRed, new JsonSerializerOptions {
+			var json = JsonSerializer.Serialize<Color> (new Color (Color.BrightRed), new JsonSerializerOptions {
 				Converters = { new ColorJsonConverter () }
 			});
 

+ 8 - 8
UnitTests/Configuration/ThemeTests.cs

@@ -38,8 +38,8 @@ namespace Terminal.Gui.ConfigurationTests {
 				{ "test",  colorScheme }
 			};
 
-			Assert.Equal ((Color)Color.Red, ((Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue) ["test"].Normal.Foreground);
-			Assert.Equal ((Color)Color.Green, ((Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue) ["test"].Normal.Background);
+			Assert.Equal (new Color (Color.Red), ((Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue) ["test"].Normal.Foreground);
+			Assert.Equal (new Color (Color.Green), ((Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue) ["test"].Normal.Background);
 
 			// Act
 			Themes.Theme = "testTheme";
@@ -47,8 +47,8 @@ namespace Terminal.Gui.ConfigurationTests {
 
 			// Assert
 			var updatedScheme = Colors.ColorSchemes ["test"];
-			Assert.Equal ((Color)Color.Red, updatedScheme.Normal.Foreground);
-			Assert.Equal ((Color)Color.Green, updatedScheme.Normal.Background);
+			Assert.Equal (new Color (Color.Red), updatedScheme.Normal.Foreground);
+			Assert.Equal (new Color (Color.Green), updatedScheme.Normal.Background);
 
 			// remove test ColorScheme from Colors to avoid failures on others unit tests with ColorScheme
 			Colors.ColorSchemes.Remove ("test");
@@ -118,10 +118,10 @@ namespace Terminal.Gui.ConfigurationTests {
 			// Assert
 			colorSchemes = (Dictionary<string, ColorScheme>)theme ["ColorSchemes"].PropertyValue;
 			// Normal should have changed
-			Assert.Equal ((Color)Color.Blue, colorSchemes ["Test"].Normal.Foreground);
-			Assert.Equal ((Color)Color.BrightBlue, colorSchemes ["Test"].Normal.Background);
-			Assert.Equal ((Color)Color.Cyan, colorSchemes ["Test"].Focus.Foreground);
-			Assert.Equal ((Color)Color.BrightCyan, colorSchemes ["Test"].Focus.Background);
+			Assert.Equal (new Color (Color.Blue), colorSchemes ["Test"].Normal.Foreground);
+			Assert.Equal (new Color (Color.BrightBlue), colorSchemes ["Test"].Normal.Background);
+			Assert.Equal (new Color (Color.Cyan), colorSchemes ["Test"].Focus.Foreground);
+			Assert.Equal (new Color (Color.BrightCyan), colorSchemes ["Test"].Focus.Background);
 		}
 
 		[Fact]

+ 24 - 24
UnitTests/Drawing/AttributeTests.cs

@@ -18,8 +18,8 @@ public class AttributeTests {
 		// Assert
 		//Assert.False (attribute.Initialized);
 		Assert.Equal (-1, attribute.PlatformColor);
-		Assert.Equal ((Color)Color.White, attribute.Foreground);
-		Assert.Equal ((Color)Color.Black, attribute.Background);
+		Assert.Equal (new Color (Color.White), attribute.Foreground);
+		Assert.Equal (new Color (Color.Black), attribute.Background);
 
 	}
 
@@ -32,8 +32,8 @@ public class AttributeTests {
 		// Assert
 		//Assert.True (attribute.Initialized);
 		Assert.Equal (42, attribute.PlatformColor);
-		Assert.Equal ((Color)Color.White, attribute.Foreground);
-		Assert.Equal ((Color)Color.Black, attribute.Background);
+		Assert.Equal (new Color (Color.White), attribute.Foreground);
+		Assert.Equal (new Color (Color.Black), attribute.Background);
 	}
 
 	[Fact]
@@ -43,8 +43,8 @@ public class AttributeTests {
 		var attribute = new Attribute (ColorName.Blue);
 
 		// Assert
-		Assert.Equal ((Color)Color.Blue, attribute.Foreground);
-		Assert.Equal ((Color)Color.Blue, attribute.Background);
+		Assert.Equal (new Color (Color.Blue), attribute.Foreground);
+		Assert.Equal (new Color (Color.Blue), attribute.Background);
 	}
 
 	[Fact, AutoInitShutdown]
@@ -70,7 +70,7 @@ public class AttributeTests {
 
 		// Assert
 		Assert.Equal (foregroundColor, attribute.Foreground);
-		Assert.Equal ((Color)backgroundColorName, attribute.Background);
+		Assert.Equal (new Color (backgroundColorName), attribute.Background);
 	}
 
 	[Fact]
@@ -82,7 +82,7 @@ public class AttributeTests {
 		var attribute = new Attribute (foregroundColorName, backgroundColor);
 
 		// Assert
-		Assert.Equal ((Color)foregroundColorName, attribute.Foreground);
+		Assert.Equal (new Color (foregroundColorName), attribute.Foreground);
 		Assert.Equal (backgroundColor, attribute.Background);
 	}
 
@@ -97,15 +97,15 @@ public class AttributeTests {
 		var attr = new Attribute ();
 
 		Assert.Equal (-1, attr.PlatformColor);
-		Assert.Equal ((Color)Color.White, attr.Foreground);
-		Assert.Equal ((Color)Color.Black, attr.Background);
+		Assert.Equal (new Color (Color.White), attr.Foreground);
+		Assert.Equal (new Color (Color.Black), attr.Background);
 
 		// Test foreground, background
 		var fg = new Color ();
-		fg = (Color)Color.Red;
+		fg = new Color (Color.Red);
 
 		var bg = new Color ();
-		bg = (Color)Color.Blue;
+		bg = new Color (Color.Blue);
 
 		attr = new Attribute (fg, bg);
 
@@ -156,8 +156,8 @@ public class AttributeTests {
 		var attribute = new Attribute (foregroundColorName, backgroundColorName);
 
 		// Assert
-		Assert.Equal ((Color)foregroundColorName, attribute.Foreground);
-		Assert.Equal ((Color)backgroundColorName, attribute.Background);
+		Assert.Equal (new Color (foregroundColorName), attribute.Foreground);
+		Assert.Equal (new Color (backgroundColorName), attribute.Background);
 	}
 
 	[Fact]
@@ -171,7 +171,7 @@ public class AttributeTests {
 		var attribute = new Attribute (foregroundColorName, backgroundColor);
 
 		// Assert
-		Assert.Equal ((Color)foregroundColorName, attribute.Foreground);
+		Assert.Equal (new Color (foregroundColorName), attribute.Foreground);
 		Assert.Equal (backgroundColor, attribute.Background);
 	}
 
@@ -187,7 +187,7 @@ public class AttributeTests {
 
 		// Assert
 		Assert.Equal (foregroundColor, attribute.Foreground);
-		Assert.Equal ((Color)backgroundColorName, attribute.Background);
+		Assert.Equal (new Color (backgroundColorName), attribute.Background);
 	}
 
 
@@ -203,10 +203,10 @@ public class AttributeTests {
 
 		var value = 42;
 		var fg = new Color ();
-		fg = (Color)Color.Red;
+		fg = new Color (Color.Red);
 
 		var bg = new Color ();
-		bg = (Color)Color.Blue;
+		bg = new Color (Color.Blue);
 
 		// Test conversion to int
 		attr = new Attribute (value, fg, bg);
@@ -223,10 +223,10 @@ public class AttributeTests {
 	public void Make_SetsNotInitialized_NoDriver ()
 	{
 		var fg = new Color ();
-		fg = (Color)Color.Red;
+		fg = new Color (Color.Red);
 
 		var bg = new Color ();
-		bg = (Color)Color.Blue;
+		bg = new Color (Color.Blue);
 
 		var a = new Attribute (fg, bg);
 
@@ -241,10 +241,10 @@ public class AttributeTests {
 		driver.Init (() => { });
 
 		var fg = new Color ();
-		fg = (Color)Color.Red;
+		fg = new Color (Color.Red);
 
 		var bg = new Color ();
-		bg = (Color)Color.Blue;
+		bg = new Color (Color.Blue);
 
 		var attr = new Attribute (fg, bg);
 		//Assert.True (attr.Initialized);
@@ -260,10 +260,10 @@ public class AttributeTests {
 	{
 
 		var fg = new Color ();
-		fg = (Color)Color.Red;
+		fg = new Color (Color.Red);
 
 		var bg = new Color ();
-		bg = (Color)Color.Blue;
+		bg = new Color (Color.Blue);
 
 		var attr = new Attribute (fg, bg);
 		//Assert.False (attr.Initialized);

+ 2 - 2
UnitTests/Drawing/ColorTests.cs

@@ -210,7 +210,7 @@ public class ColorTests {
 		var expectedColor = new Color (0, 55, 218); // Blue
 
 		// Act
-		Color color = (Color)colorName;
+		Color color = new Color (colorName);
 
 		// Assert
 		Assert.Equal (expectedColor, color);
@@ -297,7 +297,7 @@ public class ColorTests {
 		var expectedColor = new Color (197, 15, 31); // Red in RGB
 
 		// Act
-		var convertedColor = (Color)colorName;
+		var convertedColor = new Color (colorName);
 
 		// Assert
 		Assert.Equal (expectedColor, convertedColor);

+ 4 - 4
UnitTests/Text/AutocompleteTests.cs

@@ -58,11 +58,11 @@ namespace Terminal.Gui.TextTests {
 			Assert.NotSame (Colors.Menu, tv.Autocomplete.ColorScheme);
 
 			// with the values we set on it
-			Assert.Equal ((Color)Color.Black, tv.Autocomplete.ColorScheme.Normal.Foreground);
-			Assert.Equal ((Color)Color.Blue, tv.Autocomplete.ColorScheme.Normal.Background);
+			Assert.Equal (new Color (Color.Black), tv.Autocomplete.ColorScheme.Normal.Foreground);
+			Assert.Equal (new Color (Color.Blue), tv.Autocomplete.ColorScheme.Normal.Background);
 
-			Assert.Equal ((Color)Color.Black, tv.Autocomplete.ColorScheme.Focus.Foreground);
-			Assert.Equal ((Color)Color.Cyan, tv.Autocomplete.ColorScheme.Focus.Background);
+			Assert.Equal (new Color (Color.Black), tv.Autocomplete.ColorScheme.Focus.Foreground);
+			Assert.Equal (new Color (Color.Cyan), tv.Autocomplete.ColorScheme.Focus.Background);
 		}
 
 		[Fact]