Skip to content

Commit 3c7f9ed

Browse files
committed
ColorGradient fromString fix
1 parent 97707cb commit 3c7f9ed

File tree

5 files changed

+22
-33
lines changed

5 files changed

+22
-33
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
- Legend title bottomPadding extended.
8+
- ColorGradient fromString not increasing position prohibited.
89

910
### Added
1011

src/base/gfx/colorgradient.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@ namespace Gfx
1414
ColorGradient::ColorGradient(const std::string &stoplist)
1515
{
1616
for (const auto &stopString :
17-
Text::SmartString::split(stoplist, ',', true))
17+
Text::SmartString::split(stoplist, ',', true)) {
1818
if (auto &&parts =
1919
Text::SmartString::split(stopString, ' ', true);
2020
parts.size() == 2)
21-
stops.emplace_back(std::stod(parts[1]),
22-
Color::fromString(parts[0]));
23-
else
24-
throw std::logic_error(
25-
"invalid gradient stop: " + stopString);
21+
if (auto pos = std::stod(parts[1]);
22+
std::isfinite(pos)
23+
&& (stops.empty() || pos >= stops.back().pos)) {
24+
stops.emplace_back(pos, Color::fromString(parts[0]));
25+
continue;
26+
}
27+
throw std::logic_error(
28+
"invalid gradient stop: " + stopString);
29+
}
2630
}
2731

2832
ColorGradient::operator std::string() const

src/base/math/segmentedfunc.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,16 @@ template <typename T, class CRTP> struct SegmentedFunction
7373
if (stops.empty()) return T();
7474
if (stops.size() == 1 || pos < stops.front().pos)
7575
return stops.front().value;
76+
if (pos > stops.back().pos) return stops.back().value;
7677

77-
if (auto it = std::ranges::adjacent_find(stops,
78-
[pos](auto &&cur, auto &&next)
79-
{
80-
return cur.pos <= pos && pos <= next.pos;
81-
});
82-
it != stops.end())
83-
return interpolate(it->value,
84-
std::next(it)->value,
85-
Range{it->pos, std::next(it)->pos}.rescale(pos));
86-
87-
return stops.back().value;
78+
auto it = std::ranges::adjacent_find(stops,
79+
[pos](auto &&cur, auto &&next)
80+
{
81+
return cur.pos <= pos && pos <= next.pos;
82+
});
83+
return interpolate(it->value,
84+
std::next(it)->value,
85+
Range{it->pos, std::next(it)->pos}.rescale(pos));
8886
}
8987
};
9088

src/chart/main/events.h

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -373,56 +373,46 @@ class Events
373373
const std::string_view &categoryName,
374374
const std::string_view &categoryValue,
375375
const std::string &label,
376-
377376
const LegendProperties &properties)
378377
{
379378
return std::make_unique<CategoryInfo<Text<LegendChild>>>(
380379
categoryName,
381380
categoryValue,
382381
label,
383382
"label",
384-
385383
properties);
386384
}
387385

388386
static auto measLegendLabel(const std::string &label,
389-
390387
const LegendProperties &properties)
391388
{
392389
return std::make_unique<Text<LegendChild>>(label,
393390
"label",
394-
395391
properties);
396392
}
397393

398394
static auto legendTitle(const std::string &title,
399-
400395
const LegendProperties &properties)
401396
{
402397
return std::make_unique<Text<LegendChild>>(title,
403398
"title",
404-
405399
properties);
406400
}
407401

408402
static auto legendMarker(const std::string_view &categoryName,
409403
const std::string_view &categoryValue,
410-
411404
const LegendProperties &properties)
412405
{
413406
return std::make_unique<CategoryInfo<LegendChild>>(
414407
categoryName,
415408
categoryValue,
416409
"marker",
417-
418410
properties);
419411
}
420412

421413
static auto legendBar(const LegendProperties &properties)
422414
{
423-
return std::make_unique<LegendChild>("bar",
424-
425-
properties);
415+
return std::make_unique<LegendChild>("bar", properties);
426416
}
427417

428418
static auto dimAxisLabel(const std::string_view &categoryName,

src/chart/rendering/drawlegend.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ void DrawLegend::drawDimension(const Info &info) const
180180
info.dimension.category,
181181
value.second.categoryValue,
182182
value.second.categoryValue,
183-
184183
info.properties),
185184
{.alpha =
186185
double{
@@ -247,7 +246,6 @@ void DrawLegend::drawMarker(const Info &info,
247246
auto markerElement =
248247
Events::Targets::legendMarker(info.dimension.category,
249248
categoryValue,
250-
251249
info.properties);
252250

253251
if (events.marker->invoke(
@@ -307,9 +305,7 @@ void DrawLegend::extremaLabel(const Info &info,
307305
text,
308306
style.label,
309307
*events.label,
310-
Events::Targets::measLegendLabel(text,
311-
312-
info.properties),
308+
Events::Targets::measLegendLabel(text, info.properties),
313309
{.alpha = info.measureWeight * plusWeight});
314310
}
315311

0 commit comments

Comments
 (0)