Skip to content

Commit

Permalink
Clamping for CartoCSS opacity values, fixes #84
Browse files Browse the repository at this point in the history
  • Loading branch information
mtehver committed Mar 5, 2017
1 parent 239b47f commit f956a65
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
6 changes: 3 additions & 3 deletions libs-carto/mapnikvt/src/mapnikvt/MarkersSymbolizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ namespace carto { namespace mvt {
}
}
else {
vt::Color fill = _fill * _fillOpacity;
vt::Color stroke = _stroke * _strokeOpacity;
vt::Color fill = vt::Color::fromColorOpacity(_fill, _fillOpacity);
vt::Color stroke = vt::Color::fromColorOpacity(_stroke, _strokeOpacity);
if (_markerType == "ellipse" || (_markerType.empty() && placement != vt::LabelOrientation::LINE)) {
float width = DEFAULT_CIRCLE_SIZE, height = DEFAULT_CIRCLE_SIZE;
if (_widthDefined) { // NOTE: special case, if accept all values
Expand Down Expand Up @@ -130,7 +130,7 @@ namespace carto { namespace mvt {
pointInfos.clear();
}
else {
vt::BitmapLabelStyle style(orientation, vt::Color(0xffffffff) * fillOpacity, symbolizerContext.getFontManager()->getNullFont(), bitmap, transform * cglib::scale3_matrix(cglib::vec3<float>(bitmapScaleX, bitmapScaleY, 1)));
vt::BitmapLabelStyle style(orientation, vt::Color::fromColorOpacity(vt::Color(0xffffffff), fillOpacity), symbolizerContext.getFontManager()->getNullFont(), bitmap, transform * cglib::scale3_matrix(cglib::vec3<float>(bitmapScaleX, bitmapScaleY, 1)));

std::size_t labelInfoIndex = 0;
layerBuilder.addBitmapLabels([&](long long& id, vt::TileLayerBuilder::BitmapLabelInfo& labelInfo) {
Expand Down
4 changes: 2 additions & 2 deletions libs-carto/mapnikvt/src/mapnikvt/TextSymbolizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ namespace carto { namespace mvt {
std::shared_ptr<vt::Font> font;
float fontScale = symbolizerContext.getSettings().getFontScale();
if (!_faceName.empty()) {
vt::FontManager::Parameters fontParams(_size * fontScale, _fill * _opacity, _haloRadius * fontScale, _haloFill * _haloOpacity, std::shared_ptr<vt::Font>());
vt::FontManager::Parameters fontParams(_size * fontScale, vt::Color::fromColorOpacity(_fill, _opacity), _haloRadius * fontScale, vt::Color::fromColorOpacity(_haloFill, _haloOpacity), std::shared_ptr<vt::Font>());
font = symbolizerContext.getFontManager()->getFont(_faceName, fontParams);
}
else if (!_fontSetName.empty()) {
Expand All @@ -219,7 +219,7 @@ namespace carto { namespace mvt {
const std::vector<std::string>& faceNames = fontSet->getFaceNames();
for (auto it = faceNames.rbegin(); it != faceNames.rend(); it++) {
const std::string& faceName = *it;
vt::FontManager::Parameters fontParams(_size * fontScale, _fill * _opacity, _haloRadius * fontScale, _haloFill * _haloOpacity, font);
vt::FontManager::Parameters fontParams(_size * fontScale, vt::Color::fromColorOpacity(_fill, _opacity), _haloRadius * fontScale, vt::Color::fromColorOpacity(_haloFill, _haloOpacity), font);
std::shared_ptr<vt::Font> mainFont = symbolizerContext.getFontManager()->getFont(faceName, fontParams);
if (mainFont) {
font = mainFont;
Expand Down
4 changes: 2 additions & 2 deletions libs-carto/mapnikvt/src/mapnikvt/TorqueMarkerSymbolizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ namespace carto { namespace mvt {
height = bitmap->height;
}
else {
vt::Color fill = _fill * _fillOpacity;
vt::Color stroke = _stroke * _strokeOpacity;
vt::Color fill = vt::Color::fromColorOpacity(_fill, _fillOpacity);
vt::Color stroke = vt::Color::fromColorOpacity(_stroke, _strokeOpacity);
if (_markerType == "rectangle") {
std::string file = "__torque_marker_rectangle_" + boost::lexical_cast<std::string>(width) + "_" + boost::lexical_cast<std::string>(height) + "_" + boost::lexical_cast<std::string>(fill.value()) + "_" + boost::lexical_cast<std::string>(_strokeWidth) + "_" + boost::lexical_cast<std::string>(stroke.value()) + ".bmp";
bitmap = symbolizerContext.getBitmapManager()->getBitmap(file);
Expand Down
11 changes: 11 additions & 0 deletions libs-carto/vt/src/vt/Color.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <cstdint>
#include <algorithm>
#include <utility>
#include <array>

#include <cglib/vec.h>
Expand Down Expand Up @@ -57,6 +58,16 @@ namespace carto { namespace vt {
return components8;
}

static Color fromColorOpacity(const Color& baseColor, float opacity) {
Color color = baseColor;
if (opacity < 1.0f) {
for (std::size_t i = 0; i < 4; i++) {
color._components[i] *= std::max(opacity, 0.0f);
}
}
return color;
}

private:
float _components[4]; // rgba
};
Expand Down
4 changes: 2 additions & 2 deletions libs-carto/vt/src/vt/GLTileRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ namespace carto { namespace vt {

std::size_t verticesSize = _labelVertices.size();
label->calculateVertexData(_viewState, _labelVertices, _labelTexCoords, _labelIndices);
Color color = label->getColor() * label->getOpacity();
Color color = Color::fromColorOpacity(label->getColor(), label->getOpacity());
_labelColors.fill(color.rgba(), _labelVertices.size() - verticesSize);

if (_labelVertices.size() >= 32768) { // flush the batch if largest vertex index is getting 'close' to 64k limit
Expand Down Expand Up @@ -1803,7 +1803,7 @@ namespace carto { namespace vt {

std::array<cglib::vec4<float>, TileGeometry::StyleParameters::MAX_PARAMETERS> colors;
for (int i = 0; i < styleParams.parameterCount; i++) {
Color color = (*styleParams.colorTable[i])(_viewState) * (blend * opacity * (*styleParams.opacityTable[i])(_viewState));
Color color = Color::fromColorOpacity((*styleParams.colorTable[i])(_viewState) * blend, opacity * (*styleParams.opacityTable[i])(_viewState));
colors[i] = color.rgba();
}

Expand Down

0 comments on commit f956a65

Please sign in to comment.