Skip to content

Commit

Permalink
allow the user to define and use its own colormaps.
Browse files Browse the repository at this point in the history
Aladin.getListOfColormaps() and Aladin.addColormap new method. Solve #174
  • Loading branch information
bmatthieu3 committed Jul 29, 2024
1 parent d8ff8aa commit cedea78
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 32 deletions.
5 changes: 3 additions & 2 deletions examples/al-save-colormap.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
<body>

<div id="aladin-lite-div" style="width: 1024px; height: 768px"></div>

<script>var aladin;</script>
<script type="module">
import A from '../src/js/A.js';
var aladin;
A.init.then(() => {
aladin = A.aladin('#aladin-lite-div', {fullScreen: true, cooFrame: "ICRSd", showSimbadPointerControl: true, showShareControl: true, showShareControl: true, survey: 'https://alasky.cds.unistra.fr/DSS/DSSColor/', fov: 180, showContextMenu: true});
// manage URL parameters
Expand All @@ -27,6 +26,8 @@
aladin.setImageLayer(survey2);

aladin.removeHiPSFromFavorites(survey3);

aladin.addColormap('mycmap', ["lightblue", "red", "violet", "lightgreen"])
});
</script>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"homepage": "https://aladin.u-strasbg.fr/",
"name": "aladin-lite",
"type": "module",
"version": "3.4.5",
"version": "3.4.6-beta",
"description": "An astronomical HiPS visualizer in the browser",
"author": "Thomas Boch and Matthieu Baumann",
"license": "GPL-3",
Expand Down
3 changes: 2 additions & 1 deletion src/core/al-core/src/colormap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ impl Colormaps {
if let Some(id) = self.get_id(label).map(|id| *id) {
&self.cmaps[id as usize]
} else {
let id_greys = self.get_id(label).map(|id| *id).unwrap_abort();
crate::log::console_warn(&format!("{:?} is not a valid colormap, replaced with 'grayscale'.", label));
let id_greys = self.get_id("grayscale").map(|id| *id).unwrap_abort();
&self.cmaps[id_greys as usize]
}
}
Expand Down
4 changes: 0 additions & 4 deletions src/js/A.js
Original file line number Diff line number Diff line change
Expand Up @@ -954,10 +954,6 @@ A.box = function(options) {
return new Box(options)
}

A.getAvailableListOfColormaps = function() {
return ColorCfg.COLORMAPS;
};

/**
* Returns utils object
*
Expand Down
36 changes: 35 additions & 1 deletion src/js/Aladin.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ export let Aladin = (function () {
this.view.setProjection(projection);

ALEvent.PROJECTION_CHANGED.dispatchedTo(this.aladinDiv, {
projection: projection,
projection,
});
};

Expand Down Expand Up @@ -2915,6 +2915,40 @@ aladin.displayFITS(

Aladin.prototype.displayPNG = Aladin.prototype.displayJPG;

/**
* Add a custom colormap from a list of colors
*
* @memberof Aladin
*
* @returns - The list of all the colormap labels
*/
Aladin.prototype.getListOfColormaps = function() {
return this.view.wasm.getAvailableColormapList();
};

/**
* Add a custom colormap from a list of colors
*
* @memberof Aladin
* @param {string} label - The label of the colormap
* @param {string[]} colors - A list string colors
*
* @example
*
* aladin.addColormap('mycmap', ["lightblue", "red", "violet", "#ff00aaff"])
*/
Aladin.prototype.addColormap = function(label, colors) {
colors = colors.map((label) => {
return new Color(label).toHex() + 'ff';
});

this.view.wasm.createCustomColormap(label, colors);

ALEvent.UPDATE_CMAP_LIST.dispatchedTo(this.aladinDiv, {
cmaps: this.getListOfColormaps()
});
};

/*
Aladin.prototype.setReduceDeformations = function (reduce) {
this.reduceDeformations = reduce;
Expand Down
8 changes: 4 additions & 4 deletions src/js/ColorCfg.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@
/// colormap
// Make it case insensitive
colormap = colormap.toLowerCase();
if (!ColorCfg.COLORMAPS.includes(colormap)) {
/*if (!ColorCfg.COLORMAPS.includes(colormap)) {
console.warn("The colormap \'" + colormap + "\' is not supported. You should use one of the following: " + ColorCfg.COLORMAPS + "\n\'grayscale\' has been chosen by default.")
// If the user specify a colormap that is not supported,
// then set it to grayscale
colormap = "grayscale";
}
}*/

return colormap;
}
Expand Down Expand Up @@ -227,7 +227,7 @@
return [this.minCut, this.maxCut];
};

ColorCfg.COLORMAPS = [
/*ColorCfg.COLORMAPS = [
"blues",
"cividis",
"cubehelix",
Expand All @@ -251,7 +251,7 @@
"red",
"green",
"blue"
];
];*/

return ColorCfg;
})();
1 change: 0 additions & 1 deletion src/js/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import { ObsCore } from "./vo/ObsCore.js";
import { DefaultActionsForContextMenu } from "./DefaultActionsForContextMenu.js";
import { Layout } from "./gui/Layout.js";
import { SAMPActionButton } from "./gui/Button/SAMP.js";
import { HiPSCache } from "./HiPSCache.js";
import { ImageHiPS } from "./ImageHiPS.js";
import { Image } from "./Image.js";

Expand Down
2 changes: 2 additions & 0 deletions src/js/events/ALEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export class ALEvent {
static PROJECTION_CHANGED = new ALEvent("AL:projection.changed");
static FRAME_CHANGED = new ALEvent("AL:frame.changed");

static UPDATE_CMAP_LIST = new ALEvent("AL:cmap.updated");

static POSITION_CHANGED = new ALEvent("AL:position.changed");
static ZOOM_CHANGED = new ALEvent("AL:zoom.changed");

Expand Down
24 changes: 6 additions & 18 deletions src/js/gui/Box/HiPSSettingsBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ import { ColorCfg } from "../../ColorCfg.js";
type: 'select',
name: 'cmap',
value: 'native',
options: ColorCfg.COLORMAPS,
options: aladin.getListOfColormaps(),
change: (e, cmapSelector) => {
self.options.layer.setColormap(e.target.value)
},
Expand Down Expand Up @@ -280,23 +280,6 @@ import { ColorCfg } from "../../ColorCfg.js";
super.update(options)
}

_updateCursors() {

}

_show(options) {
/*if (this.selector) {
this.selector._show();
}*/

super._show(options)
}

_hide() {

super._hide()
}

_addListeners() {
ALEvent.HIPS_LAYER_CHANGED.listenedBy(this.aladin.aladinDiv, (e) => {
const hips = e.detail.layer;
Expand Down Expand Up @@ -325,6 +308,11 @@ import { ColorCfg } from "../../ColorCfg.js";
this.luminositySettingsContent.set('saturation', colorCfg.getSaturation());
}
});

ALEvent.UPDATE_CMAP_LIST.listenedBy(this.aladin.aladinDiv, (e) => {
let cmapSelector = this.colorSettingsContent.getInput('cmap');
cmapSelector.update({options: this.aladin.getListOfColormaps()})
});
}
}

0 comments on commit cedea78

Please sign in to comment.