Interactive interface for browsing global, full-resolution satellite imagery
WV-626: Palettes now update properly
Mike McGann34f98f41c942
diff --git a/etc/ant/js.build.xml b/etc/ant/js.build.xmlindex 4def18291..dcc79b511 100644--- a/etc/ant/js.build.xml+++ b/etc/ant/js.build.xml@@ -77,6 +77,7 @@<file name="js/Worldview/Models/Layers.js"/><file name="js/Worldview/Models/Projection.js"/>+ <file name="js/Worldview/Models/Palettes.js"/><file name="js/Worldview/Legacy/switch.js"/><file name="js/Worldview/Legacy/bank.js"/>diff --git a/src/index.html b/src/index.htmlindex 072a3303c..1a5cfe874 100644--- a/src/index.html+++ b/src/index.html@@ -144,6 +144,7 @@<!-- link.dev --> <script type="text/javascript" src="js/Worldview/Models/Projection.js"></script><!-- link.dev --> <script type="text/javascript" src="js/Worldview/Models/Layers.js"></script>+ <!-- link.dev --> <script type="text/javascript" src="js/Worldview/Models/Palettes.js"></script><!-- link.dev --> <script type="text/javascript" src="js/Worldview/Palette.js"></script><!-- link.dev --> <script type="text/javascript" src="js/Worldview/Palette/ColorBar.js"></script>diff --git a/src/js/Worldview/Models/Palettes.js b/src/js/Worldview/Models/Palettes.jsnew file mode 100644index 000000000..41c6752bb--- /dev/null+++ b/src/js/Worldview/Models/Palettes.js@@ -0,0 +1,23 @@+/*+ * NASA Worldview+ *+ * This code was originally developed at NASA/Goddard Space Flight Center for+ * the Earth Science Data and Information System (ESDIS) project.+ *+ * Copyright (C) 2013 United States Government as represented by the+ * Administrator of the National Aeronautics and Space Administration.+ * All Rights Reserved.+ */++Worldview.namespace("Models");++// This is juat a shell for now+Worldview.Models.Palettes = function() {++ var self = {};+ self.events = Worldview.Events();++ return self;++};+diff --git a/src/js/Worldview/Palette.js b/src/js/Worldview/Palette.jsindex 8e7bd192a..13ec64d77 100644--- a/src/js/Worldview/Palette.js+++ b/src/js/Worldview/Palette.js@@ -1,10 +1,10 @@/** NASA Worldview- *+ ** This code was originally developed at NASA/Goddard Space Flight Center for- * the Earth Science Data and Information System (ESDIS) project.+ * the Earth Science Data and Information System (ESDIS) project.*- * Copyright (C) 2013 United States Government as represented by the+ * Copyright (C) 2013 United States Government as represented by the* Administrator of the National Aeronautics and Space Administration.* All Rights Reserved.*/@@ -16,58 +16,58 @@Worldview.namespace("Palette");$(function() {-+// This namespacevar ns = Worldview.Palette;-+//-------------------------------------------------------------------------// Public//--------------------------------------------------------------------------+/*** Property: checkerboard* A canvas pattern of a gray checkerboard used to denote transparency.*/ns.checkerboard = null;-+/*** Function: toIndexedLookup* Converts a <Palette> to a <IndexedLookup>. The lookup table is generated* for a certain number of equally spaced bins. Each bin is assigned* a distance along the range and its color is determined by the stop* locations in the palette. The first bin is always at 0% and the last- * bin is always at 100%.- *+ * bin is always at 100%.+ ** If the type is gradient, the color value will be interpolated from the* stops that it fits within. If it is before the first stop, it will be* the color of the first stop. If it is after the last stop, it will be- * the color of the last stop. Colors can be interpolated in the HSL+ * the color of the last stop. Colors can be interpolated in the HSL* color space (by default or if type is "hsl") or in the RGB color space* (if the type is "rgb"). If the type is "solid", the color will be equal- * to the stop it rounds down to.- *- * Alpha values in the stop definitions are ignored. If the bin is not+ * to the stop it rounds down to.+ *+ * Alpha values in the stop definitions are ignored. If the bin is not* within the min and max values, the alpha is set to zero, otherwise the* alpha is set to 0xff.- *+ ** Parameters:* bins - Number of entries that the output lookup table should contain* palette - The <Palette> to convert- *+ ** Returns:* A <IndexedLookup>.*/- ns.toIndexedLookup = function(bins, palette, binStops) {+ ns.toIndexedLookup = function(bins, palette, binStops) {var stops = palette.stops;var lut = [];-+// Each bin in the sequence must be at the current stop or later.// Save the current index and not iterate through each time.var currentStop = 0;-+var min = palette.min || 0;var max = ( palette.max === undefined ) ? 1 : palette.max;-+for ( var bin = 0; bin < bins; bin++ ) {// Percentange this bin is located at along the range.var distance;@@ -75,10 +75,10 @@ $(function() {// Select the middle of the bin when trying to fit this invar nextStop = ( bin < bins - 1 ) ? binStops[bin + 1] : 1.0;distance = binStops[bin] + ((nextStop - binStops[bin]) / 2.0);- } else {- distance = bin / (bins - 1);+ } else {+ distance = bin / (bins - 1);}-+// If the current distance is greater than the current stop,// keep advancing until that is not true.for ( var i = currentStop; i < stops.length; i++ ) {@@ -87,17 +87,17 @@ $(function() {}}currentStop = i;-- // The bin is between these two stops++ // The bin is between these two stopsvar end = stops[Worldview.clampIndex(stops, i)];var begin = stops[Worldview.clampIndex(stops, i-1)];-+// Find out how far this bin in between the stopsvar segmentLength = end.at - begin.at;- var segmentDistance = (segmentLength !== 0) ?+ var segmentDistance = (segmentLength !== 0) ?(distance - begin.at) / segmentLength : 0;-- // Within the cutoffs?++ // Within the cutoffs?if ( distance < min || distance > max ) {lut.push({r: 0, b: 0, g: 0, a: 0});} else if ( palette.type === "solid" ) {@@ -105,15 +105,15 @@ $(function() {// stop unless we are at the very end.if ( segmentDistance < 1 ) {lut.push({- r: begin.r,- g: begin.g,- b: begin.b,+ r: begin.r,+ g: begin.g,+ b: begin.b,a: 0xff- });+ });} else {lut.push({- r: end.r,- g: end.g,+ r: end.r,+ g: end.g,b: end.b,a: 0xff});@@ -123,43 +123,43 @@ $(function() {} else { // interpolate === "hsl"lut.push(ns.hslInterpolate(segmentDistance, begin, end));}- }+ }return lut;};-+/*** Function: toColorLookup* Maps an indexed lookup to a color-to-color lookup. This is used to take* the palette definition of a rendered product to create a lookup table- * to map the original colors to a new set of colors.- *+ * to map the original colors to a new set of colors.+ ** Parameters:* indexed - An <IndexedLookup>* stops - The original color values as an array of <StopRBGA>- *+ ** Returns:- * A <ColorLookup>+ * A <ColorLookup>*/ns.toColorLookup = function(indexed, stops) {map = {};$.each(stops, function(index, stop) {var key = stop.r + "," + stop.g + "," + stop.b + "," + stop.a;- map[key] = indexed[index];+ map[key] = indexed[index];});return map;};-+/*** Function: rgbInterpolate* Interpolates a color value between two other colors via the RGB color- * space. Alpha values are ignored and are always set to 0xff.- *+ * space. Alpha values are ignored and are always set to 0xff.+ ** Parameters:* percent - The distance between the two colors as a percentage in the* range of [0.0, 1.0].* begin - The beginning <ColorRGBA>* end - The ending <ColorRGBA>- *+ ** Returns:* The interpolated color as a <ColorRGBA>.*/@@ -167,33 +167,33 @@ $(function() {return {r: Math.round(begin.r + (percent * (end.r - begin.r))),g: Math.round(begin.g + (percent * (end.g - begin.g))),- b: Math.round(begin.b + (percent * (end.b - begin.b))),- a: 0xff- }- }-+ b: Math.round(begin.b + (percent * (end.b - begin.b))),+ a: 0xff+ };+ };+/*** Function: hslInterpolate* Interpolates a color value between two other colors via the HSL* color space. Alpha values are ignored and are always set to 0xff.- *+ ** Parameters:* percent - The distance between the two colors as a percentage in the* range of [0.0, 1.0].* rgbBegin - The beginning <ColorRGBA>* rgbEnd - The ending <ColorRGBA>- *+ ** Returns:* The interpoalted color as a <ColorRGBA>.*/ns.hslInterpolate = function(percent, rgbBegin, rgbEnd) {var hslBegin = ns.rgb2hsl(rgbBegin);var hslEnd = ns.rgb2hsl(rgbEnd);-- // Hue is a circle. Traverse the shortest route. If the distance++ // Hue is a circle. Traverse the shortest route. If the distance// between the two endpoints is more than half the circle, add one// to the smallest value.- var distance = Math.abs(hslBegin.h - hslEnd.h);+ var distance = Math.abs(hslBegin.h - hslEnd.h);if ( distance > 0.5 ) {if ( hslBegin.h < hslEnd.h ) {hslBegin.h += 1.0;@@ -203,15 +203,15 @@ $(function() {}var h = hslBegin.h + (percent * (hslEnd.h - hslBegin.h));var s = hslBegin.s + (percent * (hslEnd.s - hslBegin.s));- var l = hslBegin.l + (percent * (hslEnd.l - hslBegin.l));-+ var l = hslBegin.l + (percent * (hslEnd.l - hslBegin.l));+// Normalize back to the range of 0 - 1if ( h > 1.0 ) {h -= 1.0;}return ns.hsl2rgb({h: h, s: s, l: l});- }-+ };+/*** Function: rgb2hsl* Converts an RGB color value to HSL. Conversion formula@@ -219,13 +219,13 @@ $(function() {** See Also:* http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript- *+ ** Parameters:* color - The <ColorRGBA> value to convert.- *+ ** Returns:* The converted <ColorHSL> value.- *+ ** Example:* (begin code)* >>> Worldview.Palette.rgb2hsl({r: 10, g: 20, b: 30})@@ -234,13 +234,13 @@ $(function() {*/ns.rgb2hsl = function(color) {var r = color.r;- var g = color.g+ var g = color.g;var b = color.b;-+r /= 255, g /= 255, b /= 255;var max = Math.max(r, g, b), min = Math.min(r, g, b);var h, s, l = (max + min) / 2;-+if ( max == min ) {h = s = 0; // achromatic} else {@@ -253,9 +253,9 @@ $(function() {}h /= 6;}-+return {h: h, s: s, l: l};- }+ };/*** Function: hsl2rgb@@ -264,13 +264,13 @@ $(function() {** See Also:* http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript- *+ ** Parameters:* color - The <ColorHSL> to convert.- *+ ** Returns:* The converted <ColorRGBA> value.- *+ ** Example:* (begin code)* >>> Worldview.Palette.hsl2rgb({h: 0.5833, s: 0.5, l: 0.078})@@ -281,9 +281,9 @@ $(function() {var h = color.h;var s = color.s;var l = color.l;-+var r, g, b;-+if ( s == 0 ) {r = g = b = l; // achromatic} else {@@ -295,109 +295,109 @@ $(function() {if(t < 2/3) return p + (q - p) * (2/3 - t) * 6;return p;}-+var q = l < 0.5 ? l * (1 + s) : l + s - l * s;var p = 2 * l - q;r = hue2rgb(p, q, h + 1/3);g = hue2rgb(p, q, h);b = hue2rgb(p, q, h - 1/3);}-+return {- r: Math.round(r * 255),- g: Math.round(g * 255),+ r: Math.round(r * 255),+ g: Math.round(g * 255),b: Math.round(b * 255),a: 0xff};};--++//-------------------------------------------------------------------------// Private//--------------------------------------------------------------------------- // Draws the default checkboard pattern++ // Draws the default checkboard patternvar drawCheckerboard = function() {var size = 2;-+var canvas = document.createElement("canvas");canvas.width = size * 2;canvas.height = size * 2;-+var g = canvas.getContext("2d");-+g.fillStyle = "rgb(102, 102, 102)";g.fillRect(0, 0, size, size);g.fillRect(size, size, size, size);-+g.fillStyle = "rgb(153, 153, 153)";g.fillRect(0, size, size, size);g.fillRect(size, 0, size, size);-+ns.checkerboard = g.createPattern(canvas, "repeat");- }+ };// Static initializationvar init = function() {drawCheckerboard();- }-+ };+init();});/*** Class: Worldview.Palette.ColorRGBA- *+ ** Represents a color in the RGBA color space.*- * Example:+ * Example:* For a yellow color- *+ ** (begin code)* var color = { r: 0xff, g: 0xff, b: 0x00, a: 0xff }* (end code)- *+ ** Property: r- * The red color value in the range of [0, 0xff].- *+ * The red color value in the range of [0, 0xff].+ ** Property: g* The green color value in the range of [0, 0xff].- *+ ** Property: b- * The blue color value in the range of [0, 0xff].- *+ * The blue color value in the range of [0, 0xff].+ ** Property: a- * The alpha color value in the range of [0, 0xff]. Zero is transparent, 0xff- * is opaque.- */+ * The alpha color value in the range of [0, 0xff]. Zero is transparent, 0xff+ * is opaque.+ *//*** Class: Worldview.Palette.ColorHSL- *+ ** Represents a color in the HSL color space.*- * Example:+ * Example:* For a red color- *+ ** (begin code)* var color = { h: 0x00, s: 0xff, l: 0xff }* (end code)- *+ ** Property: h* The hue value in the range of [0.0, 1.0].- *- * Property: s- * The saturation value in the range of [0.0, 1.0].- *+ *+ * Property: s+ * The saturation value in the range of [0.0, 1.0].+ ** Property: l* The lightness value in the range of [0.0, 1.0].*//*** Class: Worldview.Palette.IndexedLookup- *+ ** Indexed based color lookup table.- *+ ** Example:* (begin code)* var lookup = {@@ -409,61 +409,61 @@ $(function() {* ]* };* (end code)- *+ ** Property: id* Identifier for this lookup (optional).** Property: name* Descriptive name for this lookup (optional).- *+ ** Property: table- * Array that maps index values to <ColorRGBA> objects. For example, a value of- * 2 should use the color found at table[2].+ * Array that maps index values to <ColorRGBA> objects. For example, a value of+ * 2 should use the color found at table[2].*//*** Class: Worldview.Palette.ColorLookup- *+ ** Lookup from one color value to another color value.- *+ ** Example:- * (begin code)+ * (begin code)* var lookup = {* "0,0,255,255": { r: 0xff, g: 0x00, b: 0x00, a: 0xff }** (end code)- *+ ** Each property in the object is a string containing integer red, green,* blue, and alpha values separated by commas. Each property contains contains- * a <ColorRGBA> value to map.+ * a <ColorRGBA> value to map.*//*** Class: Worldview.Palette.StopRGBA- *+ ** Defines a color value at a percentage along a color range.- *+ ** Example:* For a red color stop at 40% along the range- *+ ** (begin code)* var stop = { at: 0.4, r: 0xff, g: 0x00, b: 0x00, a:0x00 };* (end code)- *+ ** Property: at- * The percentage along the color range where this stop is located in the range+ * The percentage along the color range where this stop is located in the range* of [0.0, 1.0].- *+ ** Property: r- * The red color value in the range of [0, 0xff].- *+ * The red color value in the range of [0, 0xff].+ ** Property: g* The green color value in the range of [0, 0xff].- *- * Property: b+ *+ * Property: b* The blue color value in the range of [0, 0xff].- *- * Property: a- * a - The alpha color value in the range of [0, 0xff]. Zero is transparent,- * 0xff is opaque.+ *+ * Property: a+ * a - The alpha color value in the range of [0, 0xff]. Zero is transparent,+ * 0xff is opaque.*/diff --git a/src/js/Worldview/Widget/ActiveLayers.js b/src/js/Worldview/Widget/ActiveLayers.jsindex d66304a20..64fd553ab 100644--- a/src/js/Worldview/Widget/ActiveLayers.js+++ b/src/js/Worldview/Widget/ActiveLayers.js@@ -17,6 +17,7 @@ Worldview.Widget.ActiveLayers = function(config, model, spec) {var aoi = config.aoi;var projectionModel = spec.projectionModel;var paletteWidget = spec.paletteWidget;+ var palettesModel = spec.palettesModel;var types = Worldview.LAYER_TYPES;var jsp;@@ -36,6 +37,8 @@ Worldview.Widget.ActiveLayers = function(config, model, spec) {.on("visibility", onLayerVisibility);projectionModel.events.on("change", onProjectionChanged);+ palettesModel.events+ .on("palette", onPaletteChanged);$(window).resize(resize);};@@ -310,7 +313,6 @@ Worldview.Widget.ActiveLayers = function(config, model, spec) {var moveLayer = function(event, ui) {var $target = ui.item;var $next = $target.next();- console.log("MOVE TO: " + $target.index(), "next", $next);if ( $next.length ) {model.moveBefore($target.attr("data-layer-type"),$target.attr("data-layer"), $next.attr("data-layer"));@@ -354,6 +356,16 @@ Worldview.Widget.ActiveLayers = function(config, model, spec) {}};+ var onPaletteChanged = function(palette, layerId) {+ var layer = config.layers[layerId];+ Worldview.Palette.ColorBar({+ selector: "#canvas" + Worldview.id(layer.id),+ bins: layer.bins,+ stops: layer.stops,+ palette: palette+ });+ };+init();return self;};diff --git a/src/js/Worldview/Widget/Palette.js b/src/js/Worldview/Widget/Palette.jsindex 7ac8e695c..1cce7caa1 100644--- a/src/js/Worldview/Widget/Palette.js+++ b/src/js/Worldview/Widget/Palette.js@@ -11,9 +11,10 @@Worldview.namespace("Widget");-Worldview.Widget.Palette = function(containerId, config, spec) {+Worldview.Widget.Palette = function(config, model, spec) {var self = {};+ var containerId = "palettes";var log = Logging.getLogger("Worldview.Widget.Palette");var value = "";var dialog = null;@@ -56,8 +57,12 @@ Worldview.Widget.Palette = function(containerId, config, spec) {var parts = v.split("~");$.each(parts, function(index, part) {var segments = part.split(",");- self.active[segments[0]] = segments[1];- self.inactive[segments[0]] = segments[1];+ var layerName = segments[0];+ var paletteName = segments[1];+ self.active[layerName] = paletteName;+ self.inactive[layerName] = paletteName;+ model.events.trigger("palette", self.getPalette(layerName),+ layerName);});REGISTRY.fire(self);};@@ -188,7 +193,7 @@ Worldview.Widget.Palette = function(containerId, config, spec) {setTimeout(function() {dialog.destroy();dialog = null;- dialogForLayer = null+ dialogForLayer = null;}, 5);});dialog.render(document.body);@@ -283,6 +288,7 @@ Worldview.Widget.Palette = function(containerId, config, spec) {self.inactive[layer] = palette.id;log.debug("Palette: " + palette.id);}+ model.events.trigger("palette", palette, layer);REGISTRY.fire(self);});diff --git a/src/js/main.js b/src/js/main.jsindex 6ab444a89..a17c25fc1 100644--- a/src/js/main.js+++ b/src/js/main.js@@ -128,6 +128,7 @@ $(function() {// Initialize "static" varsdebuggingFeatures(config);// Models+ var palettesModel = Worldview.Models.Palettes();var projectionModel = Worldview.Models.Projection(config);var layersModel = Worldview.Models.Layers(config, projectionModel);var dataDownloadModel = Worldview.DataDownload.Model(config, {@@ -150,13 +151,14 @@ $(function() {// Initialize "static" vars// Create widgetsvar projection = Worldview.Widget.Projection(projectionModel);- var palettes = Worldview.Widget.Palette("palettes", config, {+ var palettes = Worldview.Widget.Palette(config, palettesModel, {alignTo: "#products"});var layerSideBar = Worldview.Widget.LayerSideBar(layersModel);var activeLayers = Worldview.Widget.ActiveLayers(config, layersModel, {projectionModel: projectionModel,- paletteWidget: palettes+ paletteWidget: palettes,+ palettesModel: palettesModel});var addLayers = Worldview.Widget.AddLayers(config, layersModel,projectionModel);