Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #39

Merged
merged 3 commits into from
Mar 23, 2024
Merged

Dev #39

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/LadybugDisplaySchema.Tests/Model/FromJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void VisualizationDataWithStringValuesTest()
var vData = new VisualizationData(data, null);
var legend = vData.LegendParameters;
Assert.IsTrue(legend.OrdinalDictionary == null);
Assert.IsTrue(legend.HasNone);
Assert.IsTrue(legend.HasNoneColor(out _));

data = new List<string>()
{
Expand Down
142 changes: 87 additions & 55 deletions src/LadybugDisplaySchema/ManualAdded/Model/LegendParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public double SegmentHeight2D

public double Width2D => this.Vertical ? this.SegmentWidth2D : this.SegmentWidth2D * this.SegmentCountValue;
public double Height2D => this.Vertical ? this.SegmentHeight2D * this.SegmentCountValue : this.SegmentHeight2D;
public double Height2D_None => this.SegmentHeight2D / 4;
public double Height2D_Full => this.HasNone ? Height2D + this.Height2D_None + 10 : Height2D;

public double MinMaxRange => this.MaxValue.Equals(this.MinValue)? 0 : this.MaxValue - this.MinValue; // use Equals to catch double.NaN case

Expand All @@ -66,7 +64,6 @@ public double TextHeight2D
return GetPxValue(this.Properties2d.TextHeight);
}
}
public bool HasNone => this.HasNoneColor(out _);

#endregion

Expand Down Expand Up @@ -164,7 +161,7 @@ private Legend3DParameters init3DDefault()
return this.Properties3d;
}

private List<Color> _defaultColorSet = LegendColorSet.Presets.First().Value.ToList();
private static List<Color> _defaultColorSet = LegendColorSet.Presets.First().Value.ToList();

//public System.Drawing.Rectangle GetBoundary => new System.Drawing.Rectangle(this.X, this.Y, this.Width, this.Height);

Expand All @@ -174,7 +171,11 @@ public LegendParameters SetNoneColor(Color color)
}
public bool HasNoneColor(out Color noneColor)
{
var ud = this.GetUserData();
return HasNoneColor(this.GetUserData(), out noneColor);
}

public static bool HasNoneColor(Dictionary<string, object> ud, out Color noneColor)
{
noneColor = null;
if (ud.TryGetValue("_noneColor", out var color))
{
Expand Down Expand Up @@ -207,19 +208,40 @@ internal Color GetNoneColorWithDefault()
}

private List<double> _colorDomains;
private List<double> ColorDomains()
private List<double> ColorDomains(int count)
{
var cs = this.ColorsWithDefault;
if (_colorDomains != null && _colorDomains.Count == cs.Count)
if (_colorDomains != null && _colorDomains.Count == count)
return _colorDomains;

if (cs.Count < 2)
throw new System.ArgumentException("Need at least 2 colors");
_colorDomains = GenColorDomains(count);
return _colorDomains;
}

public static List<double> GenColorDomains(int count)
{
if (count < 2)
throw new System.ArgumentException("Need at least 2 colors for color domains");

double factor = 1.0 / (cs.Count - 1);
var bounds = cs.Select((_, i) => i * factor).ToList();
_colorDomains = bounds;
return bounds;
double factor = 1.0 / (count - 1);
var d = new List<double>();
for (int i = 0; i < count; i++)
{
d.Add(i * factor);
}
return d;
}

private static bool IsNone(double value, Dictionary<double,string> ordinalDictionary)
{
// check if there is none color for legend
if (double.IsNaN(value))
return true;

// check the key mapper to see if the value matches any of key mapper which is None
if (ordinalDictionary!= null && ordinalDictionary.TryGetValue(value, out var name))
return name == VisualizationData.NoneKey;

return false;
}
/// <summary>
/// Blend between two colors based on input value.
Expand All @@ -229,37 +251,65 @@ public Color CalColor(double value, ref Dictionary<double, Color> cache)
{
if (cache.TryGetValue(value, out var c))
return c;
var newColor = CalColor(value);
var newColor = CalColor(new double[] { value }).FirstOrDefault();
cache.Add(value, newColor);
return newColor;
}

public Color CalColor(double value)
public List<Color> CalColor(IEnumerable<double> values)
{
var colors = this.ColorsWithDefault.ToList();
var colorStart = colors.First();
var colorEnd = colors.Last();
var ordinalDictionary = GetOrdinalDictionary();
var min = this.MinValue;
var max = this.MaxValue;
var colors = this.ColorsWithDefault;

var colorDomins = this.ColorDomains(colors.Count);
Color noneColor = null;
var hasNoneColor = this.HasNoneColor(out noneColor);
var hasNoneInOrdDic = hasNoneColor && ordinalDictionary.ContainsValue(VisualizationData.NoneKey);
if (hasNoneInOrdDic)
{ // remove None from the dictionary, in order to calculate the color based on min/max correctly
max -= 1;
}

return CalColor(values, ordinalDictionary, min, max, colorDomins, colors, noneColor);
}

public static List<Color> CalColor(IEnumerable<double> values, Dictionary<double, string> ordinalDictionary, double min, double max, List<double> colorDomainValues, List<Color> colors, Color noneColor)
{
var cs = colors ?? _defaultColorSet;
var cd = colorDomainValues ?? GenColorDomains(cs.Count);
var cache = new Dictionary<double, Color>();
return values.Select(_=>CalColor(_, ordinalDictionary, min, max, cd, cs, noneColor, ref cache)).ToList();
}

public static Color CalColor(double value, Dictionary<double, string> ordinalDictionary, double min, double max, List<double> colorDomainValues, List<Color> colors, Color noneColor, ref Dictionary<double, Color> cache)
{
if (cache!=null && cache.TryGetValue(value, out var c))
return c;


// check if there is none color for legend
if (double.IsNaN(value))
return GetNoneColorWithDefault();
if (IsNone(value, ordinalDictionary))
return noneColor;

var colorStart = colors.First();
var colorEnd = colors.Last();

if (value <= this.MinValue)
if (value <= min)
return colorStart;
if (value >= this.MaxValue)
if (value >= max)
return colorEnd;

var range_p = this.MinMaxRange;
var factor = range_p == 0 ? 0 : (value - this.MinValue) / range_p;
var range_p = max.Equals(min) ? 0 : max - min;
var factor = range_p == 0 ? 0 : (value - min) / range_p;

var colorDomains = ColorDomains();
var segFactor = colorDomains[1];
var segFactor = colorDomainValues[1];
var colorFactor = 0.0;
for (int i = 1; i < colorDomains.Count; i++)
for (int i = 1; i < colorDomainValues.Count; i++)
{
var cFactorBefore = colorDomains[i - 1];
var cFactor = colorDomains[i];
var cFactorBefore = colorDomainValues[i - 1];
var cFactor = colorDomainValues[i];
if (factor <= cFactor && factor >= cFactorBefore)
{
colorStart = colors[i - 1];
Expand All @@ -271,11 +321,11 @@ public Color CalColor(double value)
}

var newColor = BlendColors(colorFactor, colorStart, colorEnd);

cache?.Add(value, newColor);
return newColor;
}

private Color BlendColors(double factor, Color c1, Color c2)
private static Color BlendColors(double factor, Color c1, Color c2)
{
var red = (int)(factor * (c2.R - c1.R) + c1.R);
var green = (int)(factor * (c2.G - c1.G) + c1.G);
Expand All @@ -286,33 +336,15 @@ private Color BlendColors(double factor, Color c1, Color c2)

public Dictionary<double, string> GetOrdinalDictionary()
{
var ud = ToDictionary(this.OrdinalDictionary);
if (this.OrdinalDictionary is Dictionary<double, string> od)
return od;

var ud = Extension.ToDictionary(this.OrdinalDictionary)?.ToDictionary(_=>double.Parse( _.Key), _=>_.Value.ToString());
this.OrdinalDictionary = ud;
return ud;
}


private static Dictionary<double, string> ToDictionary(object userData)
{
if (userData is Dictionary<double, string> dd)
return dd;

var uds = new Dictionary<double, string>();
Newtonsoft.Json.Linq.JObject jObj = null;
if (userData is string)
jObj = Newtonsoft.Json.Linq.JObject.Parse(userData?.ToString());
else if (userData is Newtonsoft.Json.Linq.JObject j)
jObj = j;

if (jObj != null)
{
uds = jObj.Children()
.OfType<Newtonsoft.Json.Linq.JProperty>()
.ToDictionary(_ => double.Parse(_.Name), _ => _.Value.ToString());
}
return uds;

}

public void Reset2DBaseLocation(double X = default, double Y = default)
{
if (this.Properties2d == null)
Expand Down
Loading
Loading