From a74543a1455cebcab664208841315451537c9ae4 Mon Sep 17 00:00:00 2001 From: Jiri Drbalek Date: Thu, 16 May 2019 22:54:14 +0200 Subject: [PATCH] New raster colorizer modes linear-rgba and linear-bgra --- include/mapnik/raster_colorizer.hpp | 4 +++- src/raster_colorizer.cpp | 31 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/mapnik/raster_colorizer.hpp b/include/mapnik/raster_colorizer.hpp index 18ab8c592..debe9c2bf 100644 --- a/include/mapnik/raster_colorizer.hpp +++ b/include/mapnik/raster_colorizer.hpp @@ -61,9 +61,11 @@ class raster; enum colorizer_mode_enum : std::uint8_t { COLORIZER_INHERIT = 0, //!< The stop inherits the mode from the colorizer - COLORIZER_LINEAR = 1, //!< Linear interpolation between colors + COLORIZER_LINEAR = 1, //!< Linear interpolation between colors, each channel separately COLORIZER_DISCRETE = 2, //!< Single color for stop COLORIZER_EXACT = 3, //!< Only the exact value specified for the stop gets translated, others use the default + COLORIZER_LINEAR_RGBA = 4,//!< Linear interpolation between colors, all channels combined as RGBA value + COLORIZER_LINEAR_BGRA = 5,//!< Linear interpolation between colors, all channels combined as BGRA value colorizer_mode_enum_MAX }; diff --git a/src/raster_colorizer.cpp b/src/raster_colorizer.cpp index 237dd9329..3d5737200 100644 --- a/src/raster_colorizer.cpp +++ b/src/raster_colorizer.cpp @@ -42,6 +42,8 @@ static const char *colorizer_mode_strings[] = { "linear", "discrete", "exact", + "linear-rgba", + "linear-bgra", "" }; @@ -257,6 +259,35 @@ unsigned raster_colorizer::get_color(float val) const } break; + case COLORIZER_LINEAR_RGBA: + { + if(nextStopValue == stopValue) + { + return stopColor.rgba(); + } + + double fraction = (val - stopValue) / (nextStopValue - stopValue); + double colorStart = static_cast(stopColor.rgba()); + double colorEnd = static_cast(nextStopColor.rgba()); + outputColor = color(colorStart + fraction * (colorEnd - colorStart)); + } + break; + case COLORIZER_LINEAR_BGRA: + { + if(nextStopValue == stopValue) + { + return stopColor.rgba(); + } + + double fraction = (val - stopValue) / (nextStopValue - stopValue); + std::swap(stopColor.red_, stopColor.blue_); + std::swap(nextStopColor.red_, nextStopColor.blue_); + double colorStart = static_cast(stopColor.rgba()); + double colorEnd = static_cast(nextStopColor.rgba()); + outputColor = color(colorStart + fraction * (colorEnd - colorStart)); + std::swap(outputColor.red_, outputColor.blue_); + } + break; case COLORIZER_DISCRETE: outputColor = stopColor; break;