From f1333addd23a8f3b090d5b8fd1b1330336907ca0 Mon Sep 17 00:00:00 2001 From: kspmpink Date: Thu, 29 May 2014 14:44:07 +0100 Subject: [PATCH 1/5] Added variable height gradient range Added public variables startGradientHeight and heightGradientRange to define where the gradient color range starts and what the range covers. Changed heightGradient to include the 2 colors below 0 height but this resultst in an extra lerp from Cerulean to armygreen that i cant easily avoid :(. Updated getLegend function so that it keeps track of the changing color range and uses the cache correctly. --- SCANmap.cs | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/SCANmap.cs b/SCANmap.cs index e2883c9..1f97738 100644 --- a/SCANmap.cs +++ b/SCANmap.cs @@ -6,6 +6,8 @@ namespace SCANsat public class SCANmap { private static Color[] heightGradient = { + XKCDColors.DarkPurple, + XKCDColors.Cerulean, XKCDColors.ArmyGreen, XKCDColors.Yellow, XKCDColors.Red, @@ -13,31 +15,41 @@ public class SCANmap XKCDColors.White, XKCDColors.White }; + public static float startGradientHeight = -1500;// default range is from -1500 to 9000 + public static float heightGradientRange = 10500;// default of 10500 public static Color heightToColor(float val, int scheme) { if(scheme == 1 || SCANcontroller.controller.colours == 1) { - return Color.Lerp(Color.black, Color.white, Mathf.Clamp((val + 1500f) / 9000f, 0, 1)); + return Color.Lerp(Color.black, Color.white, Mathf.Clamp((val - startGradientHeight) / heightGradientRange, 0, 1)); } Color c = Color.black; - if(val <= 0) { - val = (Mathf.Clamp(val, -1500, 0) + 1500) / 1000f; - c = Color.Lerp(XKCDColors.DarkPurple, XKCDColors.Cerulean, val); - } else { - val = (heightGradient.Length - 2) * Mathf.Clamp(val, 0, 7500) / 7500.0f; - c = Color.Lerp(heightGradient[(int)val], heightGradient[(int)val + 1], val - (int)val); + val -= startGradientHeight; + val /= heightGradientRange; + if (val<0f) {//below gradient range + c=heightGradient[0]; + } + else if (val>1f) {//above gradient range + c=heightGradient[heightGradient.Length-1]; } + else {//in gradient range + val *= (heightGradient.Length-2); + c = Color.Lerp(heightGradient[(int)val], heightGradient[(int)val + 1], val - (int)val); + } return c; } public static Texture2D legend; - private static float legendMin, legendMax; + private static float legendMin, legendMax, legendstartGradientHeight, legendheightGradientRange; private static int legendScheme; public static Texture2D getLegend(float min, float max, int scheme) { - if(legend != null && legendMin == min && legendMax == max && legendScheme == scheme) return legend; - legend = new Texture2D(256, 1, TextureFormat.RGB24, false); + if( legend != null && legendMin == min && legendMax == max && legendScheme == scheme && + legendstartGradientHeight == startGradientHeight && legendheightGradientRange == heightGradientRange ) return legend; + legend = new Texture2D(legend_width, 1, TextureFormat.RGB24, false); legendMin = min; legendMax = max; legendScheme = scheme; + legendstartGradientHeight = startGradientHeight; + legendheightGradientRange = heightGradientRange; Color[] pix = legend.GetPixels(); for(int x=0; x<256; ++x) { float val = (x * (max - min)) / 256f + min; @@ -412,4 +424,4 @@ public void resetMap(int mode) { resetMap(); } } -} \ No newline at end of file +} From 8700d5b1e5037f3ea9422b2c00240375421d01c8 Mon Sep 17 00:00:00 2001 From: kspmpink Date: Thu, 29 May 2014 14:57:37 +0100 Subject: [PATCH 2/5] Added GUI for adjusting gradient height colors Clicking in the legend now sets the start and end position for the height color gradient. left clicking sets the lower edge and right clicking sets the upper edge. Updated gui_bigmap_build function to track mouse clicks and set new legend heights. Added a return rect to drawLegend function so i can track the mouse position in relation to the legend rect. --- SCANui.cs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/SCANui.cs b/SCANui.cs index d6b3964..36f9b74 100644 --- a/SCANui.cs +++ b/SCANui.cs @@ -975,7 +975,7 @@ private static void drawLegendLabel(Rect r, float val, float min, float max) { drawLabel(lr, Color.white, txt, false, true); } - private static void drawLegend() { + private static Rect drawLegend() { if(bigmap.mapmode == 0 && SCANcontroller.controller.legend) { GUILayout.Label("", GUILayout.ExpandWidth(true)); Rect r = GUILayoutUtility.GetLastRect(); @@ -984,7 +984,8 @@ private static void drawLegend() { for(float val=-1000f; val < 9000f; val += 1000f) { drawLegendLabel(r, val, -1500f, 9000f); } - } + return r; + }return new Rect(0,0,0,0); } private static void gui_bigmap_build(int wid) { @@ -1209,17 +1210,27 @@ private static void gui_bigmap_build(int wid) { info += " " + mlat.ToString("F") + " " + mlon.ToString("F"); // uncomment for debugging projections } } + + Rect legend_rect; if(maprect.width < 720) { GUILayout.EndHorizontal(); readableLabel(info, Color.white); - drawLegend(); + legend_rect = drawLegend(); } else { GUILayout.BeginVertical(); readableLabel(info, Color.white); - drawLegend(); + legend_rect = drawLegend(); GUILayout.EndVertical(); GUILayout.EndHorizontal(); } + bool in_gradient_legend = false; + float gradient_height = -1; + if(legend_rect.width>0f) { + float mgx = (Event.current.mousePosition.x - legend_rect.x) / legend_rect.width; + float mgy = (Event.current.mousePosition.y - legend_rect.y) / legend_rect.height; + in_gradient_legend = (mgx>=0f) && (mgx<=1f) && (mgy>=0f) && (mgy<=1f); + gradient_height = -1500f+(mgx*10500f); + } if(!notMappingToday) drawMapLabels(maprect, vessel, bigmap, data); @@ -1270,6 +1281,12 @@ private static void gui_bigmap_build(int wid) { pos_spotmap.y = Math.Max(maprect.y, Math.Min(maprect.y + maprect.height - pos_spotmap.height, pos_spotmap.y)); } } + if (in_gradient_legend) + { + SCANmap.heightGradientRange = gradient_height-SCANmap.startGradientHeight; + bigmap.resetMap(); + if(spotmap != null) spotmap.resetMap(); + } } else if(Event.current.button == 0) { if(spotmap != null && in_spotmap) { spotmap.mapscale = spotmap.mapscale / 1.25f; @@ -1277,6 +1294,12 @@ private static void gui_bigmap_build(int wid) { spotmap.resetMap(spotmap.mapmode); Event.current.Use(); } + if (in_gradient_legend) + { + SCANmap.startGradientHeight = gradient_height; + bigmap.resetMap(); + if(spotmap != null) spotmap.resetMap(); + } } Event.current.Use(); } else if(Event.current.type == EventType.MouseDown) { From 187d5629f72be482c2824eb20458f824b2af2d95 Mon Sep 17 00:00:00 2001 From: kspmpink Date: Thu, 29 May 2014 15:02:25 +0100 Subject: [PATCH 3/5] Update SCANmap.cs --- SCANmap.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SCANmap.cs b/SCANmap.cs index 1f97738..8f34e2a 100644 --- a/SCANmap.cs +++ b/SCANmap.cs @@ -44,7 +44,7 @@ public static Color heightToColor(float val, int scheme) { public static Texture2D getLegend(float min, float max, int scheme) { if( legend != null && legendMin == min && legendMax == max && legendScheme == scheme && legendstartGradientHeight == startGradientHeight && legendheightGradientRange == heightGradientRange ) return legend; - legend = new Texture2D(legend_width, 1, TextureFormat.RGB24, false); + legend = new Texture2D(256, 1, TextureFormat.RGB24, false); legendMin = min; legendMax = max; legendScheme = scheme; From ed78a3cda8af930bc0548a6711564d77c62957f2 Mon Sep 17 00:00:00 2001 From: kspmpink Date: Thu, 29 May 2014 15:18:00 +0100 Subject: [PATCH 4/5] fixed small left click bug in height gradient gui heightGradientRange is relevant to startGradientHeight and so must be recalculated when startGradientHeight is moved with a left click. --- SCANui.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SCANui.cs b/SCANui.cs index 36f9b74..5c27f66 100644 --- a/SCANui.cs +++ b/SCANui.cs @@ -1296,7 +1296,9 @@ private static void gui_bigmap_build(int wid) { } if (in_gradient_legend) { + float end_pos = SCANmap.startGradientHeight+SCANmap.heightGradientRange; SCANmap.startGradientHeight = gradient_height; + SCANmap.heightGradientRange = end_pos-SCANmap.startGradientHeight; bigmap.resetMap(); if(spotmap != null) spotmap.resetMap(); } From c6ff2b964ce7b187c530aa1d9506e064c79041a0 Mon Sep 17 00:00:00 2001 From: kspmpink Date: Fri, 30 May 2014 16:44:48 +0100 Subject: [PATCH 5/5] prevent change in colors from original Slight change to heightToColor function as to maintain the original color schema. This removes a lerp from Cerulean to ArmyGreen that i had added and realigns Cerulean with 0 meter altitude. --- SCANmap.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/SCANmap.cs b/SCANmap.cs index 8f34e2a..78ee3eb 100644 --- a/SCANmap.cs +++ b/SCANmap.cs @@ -6,17 +6,15 @@ namespace SCANsat public class SCANmap { private static Color[] heightGradient = { - XKCDColors.DarkPurple, - XKCDColors.Cerulean, XKCDColors.ArmyGreen, XKCDColors.Yellow, XKCDColors.Red, XKCDColors.Magenta, - XKCDColors.White, XKCDColors.White }; public static float startGradientHeight = -1500;// default range is from -1500 to 9000 public static float heightGradientRange = 10500;// default of 10500 + private const float gradientSealevelPercent = 1500f/10500f;//indetifys the point to start using heightGradient for lerps public static Color heightToColor(float val, int scheme) { if(scheme == 1 || SCANcontroller.controller.colours == 1) { @@ -26,14 +24,20 @@ public static Color heightToColor(float val, int scheme) { val -= startGradientHeight; val /= heightGradientRange; if (val<0f) {//below gradient range - c=heightGradient[0]; + c=XKCDColors.DarkPurple; } - else if (val>1f) {//above gradient range + else if (val>=1f) {//above gradient range c=heightGradient[heightGradient.Length-1]; } else {//in gradient range - val *= (heightGradient.Length-2); - c = Color.Lerp(heightGradient[(int)val], heightGradient[(int)val + 1], val - (int)val); + if (val<=gradientSealevelPercent) { + c = Color.Lerp(XKCDColors.DarkPurple, XKCDColors.Cerulean, val/gradientSealevelPercent ); + } else { + val -= gradientSealevelPercent;//remove sealevel chunk + val /= (1f-gradientSealevelPercent);//realign the range from 0 to 1 + val *= (heightGradient.Length-1); + c = Color.Lerp(heightGradient[(int)val], heightGradient[(int)val + 1], val - (int)val); + } } return c; }