IsoFieldViewer was changed

This commit is contained in:
Evgeny Redikultsev
2024-02-24 17:59:37 +05:00
parent 8572e1f93d
commit 541f23c0a8
16 changed files with 224 additions and 129 deletions

View File

@@ -1,5 +1,6 @@
using FieldVisualizer.Entities.ColorMaps;
using FieldVisualizer.Entities.Values;
using StructureHelperCommon.Infrastructures.Exceptions;
using System;
using System.Collections.Generic;
using System.Text;
@@ -10,54 +11,123 @@ namespace FieldVisualizer.Services.ColorServices
public static class ColorOperations
{
const byte Alpha = 0xff;
public static Color GetColorByValue(IValueRange range, IColorMap map, double val)
{
if (range.TopValue == range.BottomValue || map.Colors.Count == 0) { return map.Colors[0]; }
double minVal = range.BottomValue - 1e-15d*(Math.Abs(range.BottomValue));
double maxVal = range.TopValue + 1e-15d * (Math.Abs(range.TopValue));
if (val > maxVal || val < minVal) { return Colors.Gray; }
if (val == minVal) { return map.Colors[0]; }
if (val == maxVal) { return map.Colors[map.Colors.Count - 1]; }
double valPerc = (val - minVal) / (maxVal - minVal);// value%
if (valPerc >= 1d)
{ return map.Colors[map.Colors.Count - 1]; }
double colorPerc = 1d / (map.Colors.Count - 1d); // % of each block of color. the last is the "100% Color"
double blockOfColor = valPerc / colorPerc;// the integer part repersents how many block to skip
int blockIdx = (int)Math.Truncate(blockOfColor);// Idx of
double valPercResidual = valPerc - (blockIdx * colorPerc);//remove the part represented of block
double percOfColor = valPercResidual / colorPerc;// % of color of this block that will be filled
Color cTarget = map.Colors[blockIdx];
Color cNext = map.Colors[blockIdx + 1];
var deltaR = cNext.R - cTarget.R;
var deltaG = cNext.G - cTarget.G;
var deltaB = cNext.B - cTarget.B;
var R = cTarget.R + (deltaR * percOfColor);
var G = cTarget.G + (deltaG * percOfColor);
var B = cTarget.B + (deltaB * percOfColor);
Color c = map.Colors[0];
c = Color.FromArgb(Alpha, (byte)R, (byte)G, (byte)B);
return c;
}
/// <summary>
///
/// </summary>
/// <param name="fullRange"></param>
/// <param name="valueRanges"></param>
/// <param name="colorMap"></param>
/// <returns></returns>
public static IEnumerable<IValueColorRange> GetValueColorRanges(IValueRange fullRange, IEnumerable<IValueRange> valueRanges, IColorMap colorMap)
{
var colorRanges = new List<IValueColorRange>();
foreach (var valueRange in valueRanges)
{
IValueColorRange valueColorRange = new ValueColorRange();
valueColorRange.IsActive = true;
valueColorRange.BottomValue = valueRange.BottomValue;
valueColorRange.AverageValue = (valueRange.BottomValue + valueRange.TopValue) / 2;
valueColorRange.TopValue = valueRange.TopValue;
IValueColorRange valueColorRange = new ValueColorRange
{
IsActive = true,
BottomValue = valueRange.BottomValue,
AverageValue = (valueRange.BottomValue + valueRange.TopValue) / 2,
TopValue = valueRange.TopValue
};
valueColorRange.BottomColor = GetColorByValue(fullRange, colorMap, valueColorRange.BottomValue);
valueColorRange.TopColor = GetColorByValue(fullRange, colorMap, valueColorRange.TopValue);
colorRanges.Add(valueColorRange);
}
return colorRanges;
}
/// <summary>
/// Returns color by value, range of value an color map
/// </summary>
/// <param name="range">Range of valoue</param>
/// <param name="map">Color map</param>
/// <param name="val">Value</param>
/// <returns></returns>
public static Color GetColorByValue(IValueRange range, IColorMap map, double val)
{
CheckColorMap(map);
if (range.TopValue == range.BottomValue || map.Colors.Count == 1) //if range width is zero or map contain just 1 color
{
return map.Colors[0];
}
var valueRange = GetExtendedRange(range);
if (val >= valueRange.TopValue || val <= valueRange.BottomValue)
{
return GetColorValueIsOutOfRange(valueRange, map, val);
}
return GetColorValueIsInRange(valueRange, map, val);
}
private static Color GetColorValueIsOutOfRange(IValueRange range, IColorMap map, double val)
{
if (val > range.TopValue || val < range.BottomValue)
{
return Colors.Gray;
}
if (val == range.BottomValue)
{
return map.Colors[0];
}
if (val == range.TopValue)
{
return map.Colors[^1];
}
throw new StructureHelperException(ErrorStrings.DataIsInCorrect);
}
private static Color GetColorValueIsInRange(IValueRange range, IColorMap map, double val)
{
var deltaVal = val - range.BottomValue;
var rangeWidth = range.TopValue - range.BottomValue;
var valPerc = deltaVal / rangeWidth; // percent of value on the distance from minValue to maxValue
if (valPerc >= 1d)
{
return map.Colors[^1];
}
double colorPerc = 1d / (map.Colors.Count - 1d); // % of each block of color. the last is the "100% Color"
double blockOfColor = valPerc / colorPerc;// the integer part repersents how many block to skip
int blockIdx = (int)Math.Truncate(blockOfColor);// Idx of
double valPercResidual = valPerc - (blockIdx * colorPerc);//remove the part represented of block
double percOfColor = valPercResidual / colorPerc;// % of color of this block that will be filled
Color c = GetColorByColorMap(map, blockIdx, percOfColor);
return c;
}
private static IValueRange GetExtendedRange(IValueRange range)
{
var minVal = range.BottomValue - 1e-15d * Math.Abs(range.BottomValue);
var maxVal = range.TopValue + 1e-15d * Math.Abs(range.TopValue);
return new ValueRange()
{
BottomValue = minVal,
TopValue = maxVal
};
}
private static Color GetColorByColorMap(IColorMap map, int blockIdx, double percOfColor)
{
Color cTarget = map.Colors[blockIdx];
Color cNext = map.Colors[blockIdx + 1];
var deltaRed = cNext.R - cTarget.R;
var deltaGreen = cNext.G - cTarget.G;
var deltaBlue = cNext.B - cTarget.B;
var Red = cTarget.R + (deltaRed * percOfColor);
var Green = cTarget.G + (deltaGreen * percOfColor);
var Blue = cTarget.B + (deltaBlue * percOfColor);
Color c = Color.FromArgb(Alpha, (byte)Red, (byte)Green, (byte)Blue);
return c;
}
private static void CheckColorMap(IColorMap map)
{
if (map.Colors.Count == 0)
{
throw new StructureHelperException(ErrorStrings.DataIsInCorrect + ": Color map is empty");
}
}
}
}