Skip to content

Commit

Permalink
[Vector] Changed the gradient resolution algorithm to average out the…
Browse files Browse the repository at this point in the history
… colours.
  • Loading branch information
paul-manias committed Feb 2, 2025
1 parent 4ed5787 commit e6ac0d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
15 changes: 3 additions & 12 deletions src/vector/defs/gradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,10 @@ GradientColours::GradientColours(const std::array<FRGB, 256> &Map, double Resolu
{
resolution = Resolution;

if (resolution > 0) { // Select every nth colour from the map.
auto current = agg::rgba8(Map[0]);
LONG in = F2T(resolution * 255.0);
for (LONG i = 0; i < std::ssize(Map); i++, in--) {
if ((!in) and (i != std::ssize(Map)-1)) { current = agg::rgba8(Map[i]); in = F2T(resolution * 255.0); }
table[i] = current;
}
}
else {
for (LONG i=0; i < std::ssize(Map); i++) {
table[i] = agg::rgba8(Map[i]);
}
for (LONG i=0; i < std::ssize(Map); i++) {
table[i] = agg::rgba8(Map[i]);
}
if (Resolution > 0) apply_resolution(Resolution);
}

//********************************************************************************************************************
Expand Down
24 changes: 19 additions & 5 deletions src/vector/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -534,11 +534,25 @@ class GradientColours {

void apply_resolution(double Resolution) {
resolution = Resolution;
auto current = table[0];
LONG in = F2T(resolution * 255.0);
for (LONG i = 0; i < 256; i++, in--) {
if ((!in) and (i != 255)) { current = table[i]; in = F2T(resolution * 255.0); }
table[i] = current;

// For a given block of colours, compute the average colour and apply it to the entire block.

LONG block_size = F2T(resolution * table.size());
for (LONG i = 0; i < table.size(); i += block_size) {

LONG red = 0, green = 0, blue = 0, alpha = 0, total = 0;
for (LONG b=i; (b < i + block_size) and (b < table.size()); b++, total++) {
red += table[b].r * table[b].r;
green += table[b].g * table[b].g;
blue += table[b].b * table[b].b;
alpha += table[b].a * table[b].a;
}

auto col = agg::rgba8{
UBYTE(sqrt(red/total)), UBYTE(sqrt(green/total)), UBYTE(sqrt(blue/total)), UBYTE(sqrt(alpha/total))
};

for (LONG b = i; (b < i + block_size) and (b < table.size()); b++) table[b] = col;
}
}
};
Expand Down

0 comments on commit e6ac0d3

Please sign in to comment.