我想知道如何将字符串转换为我已在c#中设置的颜色?我有一个类从列表中设置颜色,颜色变量名称设置为字符串。我有对象设置为颜色,但我想知道如何使用颜色转换器为其他对象设置颜色,我意识到这种描述很差,所以希望我的图像将有助于解释更多。也希望有人可以帮助我:)。
图片1是我当前的输出
我希望“区域图例”下的颜色与“层序地层学”相同
序列straigraphy的颜色设置的我的代码以及我的区域传奇代码
层序地层颜色
internal static class ZoneColorizer
{
public static void Colorize(ZoneDto[] zones)
{
Color[] colorsToChooseFrom = new Color[] { Colors.DarkSlateGray, Colors.DarkBlue, Colors.DarkRed, Colors.DarkCyan, Colors.DarkGoldenrod, Colors.DarkGreen, Colors.DarkMagenta };
byte colorIndex = 0;
foreach (var zone in zones)
{
var color = colorsToChooseFrom[colorIndex];
var zoneColor = Color.FromArgb(128, color.R, color.G, color.B);
zone.ColourCode = zoneColor.ToString();
colorIndex++;
byte subzoneNumber = 50;
byte totalSubZones = (byte)zone.Subzones.LongLength;
byte multipler = (byte)(200 / totalSubZones);
foreach (var subzone in zone.Subzones)
{
var subZoneColor = Color.FromArgb(subzoneNumber, color.R, color.G, color.B);
subzone.ColourCode = subZoneColor.ToString();
subzoneNumber+= multipler;
}
}
}
}
区域图例代码
private List<Color> colours;
public ObservableCollection<string> Zones { get; set; }
public LegendControl()
{
colours = new List<Color>() { Colors.DarkSlateGray, Colors.DarkBlue, Colors.DarkRed, Colors.DarkCyan, Colors.DarkGoldenrod, Colors.DarkGreen, Colors.DarkMagenta };
InitializeComponent();
Zones = new ObservableCollection<string>();
DataContext = Zones;
}
public void Bind(ZoneDto[] zones)
{
int zoneNumber = 0;
int rowNumber = 0;
byte subzoneNumber = 0;
byte totalsubZones = 0;
Zones.Clear();
foreach (var zone in zones)
{
Color zoneColor = GetColour(zoneNumber);
var zonerectangle = CreateZoneRectangle(zone, rowNumber, zoneColor);
Grid1.Children.Add(zonerectangle);
var zoneName = CreateZoneName(zone, rowNumber);
Grid1.Children.Add(zoneName);
zoneNumber++;
foreach (var subzone in zone.Subzones)
{
totalsubZones++;
Grid1.RowDefinitions.Add(new RowDefinition());
Color subzoneColor = GetGradeColour(subzoneNumber, totalsubZones, zoneColor);
var subzoneRectangle = CreateSubZoneRectangle(subzone, rowNumber, subzoneColor);
Grid1.Children.Add(subzoneRectangle);
var subzoneName = CreateSubZoneName(subzone, rowNumber);
Grid1.Children.Add(subzoneName);
subzoneNumber++;
rowNumber++;
}
}
}
private Rectangle CreateZoneRectangle(ZoneDto zone, int rowNumber, Color zoneColour)
{
var rectangle = new Rectangle
{
Fill = new SolidColorBrush(zoneColour),
Margin = new Thickness(2)
};
int rowSpan = 0;
foreach (var subzone in zone.Subzones)
{
rowSpan++;
}
rectangle.SetValue(Grid.RowProperty, rowNumber);
rectangle.SetValue(Grid.ColumnProperty, 0);
rectangle.SetValue(Grid.RowSpanProperty, rowSpan);
return rectangle;
}
private TextBlock CreateZoneName(ZoneDto zone, int rowNumber)
{
Zones.Clear();
var textblock = new TextBlock
{
Text = zone.Name,
RenderTransformOrigin = new Point(0.5, 0.5),
LayoutTransform = new RotateTransform(270),
VerticalAlignment = System.Windows.VerticalAlignment.Center,
HorizontalAlignment = System.Windows.HorizontalAlignment.Center
};
int rowSpan = 0;
foreach (var subzon in zone.Subzones)
{
rowSpan++;
}
textblock.SetValue(Grid.RowProperty, rowNumber);
textblock.SetValue(Grid.ColumnProperty, 0);
textblock.SetValue(Grid.RowSpanProperty, rowSpan);
return textblock;
}
private Rectangle CreateSubZoneRectangle(ZoneDto subzone, int rowNumber, Color subzoneColor)
{
var rectangle = new Rectangle
{
Fill = new SolidColorBrush(subzoneColor),
Margin = new Thickness(2)
};
rectangle.SetValue(Grid.RowProperty, rowNumber);
rectangle.SetValue(Grid.ColumnProperty, 1);
return rectangle;
}
private TextBlock CreateSubZoneName(ZoneDto subzone, int rowNumber)
{
var textblock = new TextBlock
{
Text = subzone.Name,
VerticalAlignment = System.Windows.VerticalAlignment.Center,
HorizontalAlignment = System.Windows.HorizontalAlignment.Center
};
textblock.SetValue(Grid.RowProperty, rowNumber);
textblock.SetValue(Grid.ColumnProperty, 1);
return textblock;
}
private Color GetColour(int zoneNumber)
{
var numOfColours = colours.Count;
if (zoneNumber >= numOfColours)
{
return colours[zoneNumber % numOfColours];
}
return colours[zoneNumber];
}
private Color GetGradeColour(byte subzoneNumber, byte totalsubZones, Color zoneColour)
{
byte multipler = (byte)(200 / totalsubZones);
byte a = (byte)((subzoneNumber + 1) * multipler);
return Color.FromArgb(a, zoneColour.R, zoneColour.G, zoneColour.B);
}
ZoneDto的代码
public class GridDataDto
{
public WellDto[] Wells { get; set; }
public ZoneDto[] Zones { get; set; }
public string[] Facies { get; set; }
public CellDto MinLimits { get; set; }
public CellDto MaxLimits { get; set; }
}
public class ZoneDto
{
public string Name { get; set; }
public ZoneDto[] Subzones { get; set; }
public int MinK { get; set; }
public int MaxK { get; set; }
public string ColourCode { get; set; }
}
public class WellDto
{
public string Name { get; set; }
public double X { get; set; }
public double Y { get; set; }
}
public class CellDto
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
public int I { get; set; }
public int J { get; set; }
public int K { get; set; }
}
答案 0 :(得分:1)
编辑:似乎已有针对此ColorConverter.ConvertFromString
https://msdn.microsoft.com/en-us/library/system.windows.media.colorconverter.convertfromstring(v=vs.110).aspx的内置方法
无论如何,这是旧代码 -
System.Windows.Media.Color
ToString()方法返回aRGB值的十六进制字符串,如下所示:#80C8DCF0
您可以将字节分开并创建一个新颜色:
static Color FromString(string colorString)
{
byte a = Convert.ToByte(colorString.Substring(1, 2), 16);
byte r = Convert.ToByte(colorString.Substring(3, 2), 16);
byte g = Convert.ToByte(colorString.Substring(5, 2), 16);
byte b = Convert.ToByte(colorString.Substring(7, 2), 16);
return Color.FromArgb(a, r, g, b);
}
答案 1 :(得分:0)
我可能会错误地阅读您的问题,但如果您只是希望图例与“序列地层”共享相同的颜色,那么您不能只使用IBAction
的{{1}}方法{区域图例代码的ZoneColorizer
方法中的{1}}对象?