Skip to content

Commit

Permalink
This alters how the monochrome and monoval colour maps work. These ca…
Browse files Browse the repository at this point in the history
…n now vary from white(black) to a destination colour (rather than all the way up to the max sat/val of a destination hue)
  • Loading branch information
sebjameswml committed Oct 7, 2024
1 parent 6132617 commit 92c0183
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
45 changes: 45 additions & 0 deletions examples/colourmaps_mono.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,51 @@ int main()
}
}

cmap_types.clear();
cmap_types.push_back (morph::ColourMapType::Monochrome);
cmap_types.push_back (morph::ColourMapType::Monochrome);
cmap_types.push_back (morph::ColourMapType::Monochrome);
cmap_types.push_back (morph::ColourMapType::Monochrome);
cmap_types.push_back (morph::ColourMapType::Monoval);
cmap_types.push_back (morph::ColourMapType::Monoval);

std::array<float, 3> purple = morph::vec<int, 3>({0x68, 0x31, 0x92}).as_float()/255.0f;
std::array<float, 3> orange = morph::vec<int, 3>({0xdf, 0x5e, 0x26}).as_float()/255.0f;
std::array<float, 3> blue = morph::vec<int, 3>({0x2a, 0x37, 0x91}).as_float()/255.0f;
std::array<float, 3> green = morph::vec<int, 3>({0x5b, 0x89, 0x3d}).as_float()/255.0f;
//std::array<float, 3> purple2 = morph::vec<int, 3>({0xa4, 0x84, 0xbc}).as_float()/255.0f;
std::array<float, 3> orange2 = morph::vec<int, 3>({0xee, 0x9f, 0x7d}).as_float()/255.0f;

std::vector<std::array<float, 3>> clrs;
clrs.push_back (purple);
clrs.push_back (orange);
clrs.push_back (green);
clrs.push_back (blue);
clrs.push_back (orange);
clrs.push_back (orange2);

int j = 0;
for (auto cmap_type : cmap_types) {
++i;
cm1.setType (cmap_type);
cm1.setRGB (clrs[j++]);
auto cbv = std::make_unique<morph::ColourBarVisual<float>>(offset);
v.bindmodel (cbv);
cbv->orientation = morph::colourbar_orientation::vertical;
cbv->tickside = morph::colourbar_tickside::right_or_below;
cbv->cm = cm1;
cbv->scale = scale1;
// morph::ColourMap<float>::colourMapTypeToStr (cmap_type)
cbv->addLabel (std::format("hue={:.2f}", cbv->cm.getHue()), {0, -0.1, 0}, morph::TextFeatures(0.05f));
cbv->finalize();
v.addVisualModel (cbv);
// Update location
offset[0] += 0.4f;
if (i % 6 == 0) {
offset[0] = 0.0f;
offset[1] -= 1.0f;
}
}

v.keepOpen();

Expand Down
51 changes: 39 additions & 12 deletions morph/ColourMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ namespace morph {
Twilight,
Greyscale, // Greyscale is any hue; saturation=0; *value* varies. High signal (datum ->1) gives dark greys to black
GreyscaleInv, // Inverted Greyscale. High signal gives light greys to white
Monochrome, // Monochrome is 'monohue': fixed hue; vary the *saturation* with value fixed at 1.
Monochrome, // Monochrome is 'monohue': fixed hue and value; vary the *saturation* from 0 to this->sat.
MonochromeRed,
MonochromeBlue,
MonochromeGreen,
Monoval, // Monoval varies the *value* of the colour
Monoval, // Monoval varies the *value* of the colour with fixed hue and saturation.
MonovalRed,
MonovalBlue,
MonovalGreen,
Expand Down Expand Up @@ -85,9 +85,9 @@ namespace morph {
ColourMapType type = ColourMapType::Plasma;
//! The hue (range 0 to 1.0f) as used in HSV colour values for Monochrome maps.
float hue = 0.0f;
//! The saturation, used for ColourMapType::Fixed only
//! The saturation
float sat = 1.0f;
//! The value, used for ColourMapType::Fixed only
//! The value
float val = 1.0f;

// Used by Duochrome
Expand Down Expand Up @@ -725,18 +725,24 @@ namespace morph {
case ColourMapType::MonovalRed:
{
this->hue = 1.0f;
this->val = 1.0f;
this->sat = 1.0f;
break;
}
case ColourMapType::MonochromeBlue:
case ColourMapType::MonovalBlue:
{
this->hue = 0.667f;
this->val = 1.0f;
this->sat = 1.0f;
break;
}
case ColourMapType::MonochromeGreen:
case ColourMapType::MonovalGreen:
{
this->hue = 0.333f;
this->val = 1.0f;
this->sat = 1.0f;
break;
}
case ColourMapType::HSV1D:
Expand Down Expand Up @@ -901,26 +907,31 @@ namespace morph {
//! but for fixed, it allows you to have white, with hue=anything, sat=0, val=1
void setSat (const float& _s)
{
if (this->type != ColourMapType::Fixed) {
throw std::runtime_error ("Only ColourMapType::Fixed allows setting of saturation");
if (this->type != ColourMapType::Fixed && this->type != ColourMapType::Monochrome && this->type != ColourMapType::Monoval) {
throw std::runtime_error ("Only ColourMapType::Fixed ::Monochrome and ::Monoval allow setting of saturation");
}
this->sat = _s;
}

//! Set just the colour's value (ColourMapType::Fixed/HSV only)
void setVal (const float& _v)
{
if (this->type != ColourMapType::Fixed && this->type != ColourMapType::HSV) {
throw std::runtime_error ("Only ColourMapType::Fixed and ColourMapType::HSV allow setting of value");
if (this->type != ColourMapType::Fixed && this->type != ColourMapType::HSV
&& this->type != ColourMapType::Monochrome && this->type != ColourMapType::Monoval) {
throw std::runtime_error ("Only ColourMapType::Fixed ::HSV ::Monochrome and ::Monoval allow setting of value");
}
this->val = _v;
}

float getHue() const { return this->hue; }
float getSat() const { return this->sat; }
float getVal() const { return this->val; }

//! Set the colour by hue, saturation and value (ColourMapType::Fixed only)
void setHSV (const float& h, const float& s, const float& v)
{
if (this->type != ColourMapType::Fixed) {
throw std::runtime_error ("Only ColourMapType::Fixed allows setting of saturation/value");
if (this->type != ColourMapType::Fixed && this->type != ColourMapType::Monochrome && this->type != ColourMapType::Monoval) {
throw std::runtime_error ("Only ColourMapType::Fixed/Monochrome/Monoval allows setting of saturation/value");
}
this->hue = h;
this->sat = s;
Expand All @@ -930,6 +941,15 @@ namespace morph {
//! Set the colour by hue, saturation and value (defined in an array) (ColourMapType::Fixed only)
void setHSV (const std::array<float,3> hsv) { this->setHSV (hsv[0],hsv[1],hsv[2]); }

//! Set this->hue, sat and val from the passed in RGB triplet
void setRGB (const std::array<float,3> rgb)
{
std::array<float,3> hsv = ColourMap<T>::rgb2hsv (rgb);
this->hue = hsv[0];
this->sat = hsv[1];
this->val = hsv[2];
}

//! Get the hue, in its most saturated form
std::array<float, 3> getHueRGB() const { return ColourMap::hsv2rgb (this->hue, 1.0f, 1.0f); }

Expand Down Expand Up @@ -1077,18 +1097,24 @@ namespace morph {

if (hsv[0] < 0.0f) { hsv[0] += 360.0f; }

hsv[0] /= 360.0f; // Finally convert back to a range 0 to 1, compatible with this->hue.

return hsv;
}

private:
/*!
* @param datum gray value from 0.0 to 1.0
*
* The idea is to range from the value hsv to white. That means as datum nears 0, val should go from val towards 1.0 (max white)
*
* @returns RGB value in a mono-colour map, with main colour this->hue; varying saturation.
*/
std::array<float,3> monochrome (float datum) const
{
return ColourMap::hsv2rgb (this->hue, datum, 1.0f);
float _datum = datum * this->sat; // _datum ranges between 0 and this->sat for input [0, 1]
float _val = this->val + (1.0f - this->val) * (1.0f - datum); // val goes to 1 as input nears 0 (ensuring white)
return ColourMap::hsv2rgb (this->hue, _datum, _val);
}

/*!
Expand All @@ -1098,7 +1124,8 @@ namespace morph {
*/
std::array<float,3> monoval (float datum) const
{
return ColourMap::hsv2rgb (this->hue, 1.0f, datum);
float _datum = datum * this->val;
return ColourMap::hsv2rgb (this->hue, this->sat, _datum);
}

/*!
Expand Down

0 comments on commit 92c0183

Please sign in to comment.