Skip to content

Commit 2107459

Browse files
committed
Add updateColor() to PenState
1 parent 19e2095 commit 2107459

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/penstate.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ struct PenState
2727
this->brightness = hsvColor.valueF() * 100;
2828
this->transparency = 100 * (1 - hsvColor.alphaF());
2929
}
30+
31+
void updateColor()
32+
{
33+
const int h = std::round(std::fmod(color * 360 / 100, 360.0));
34+
const int s = std::round(saturation * 2.55);
35+
const int v = std::round(brightness * 2.55);
36+
const int a = std::round((100 - transparency) * 2.55);
37+
38+
penAttributes.color = QColor::fromHsv(h, s, v, a);
39+
}
3040
};
3141

3242
} // namespace scratchcpprender

test/penstate/penstate_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ TEST(PenStateTest, SetColor)
3131
ASSERT_EQ(std::round(state.transparency * 100) / 100, 34.51);
3232
ASSERT_EQ(state.shade, 50);
3333
}
34+
35+
TEST(PenStateTest, UpdateColor)
36+
{
37+
PenState state;
38+
state.color = 78.64;
39+
state.saturation = 45.07;
40+
state.brightness = 12.5;
41+
state.transparency = 36.09;
42+
state.shade = 85;
43+
state.updateColor();
44+
ASSERT_EQ(state.penAttributes.color, QColor::fromHsv(283, 115, 32, 163));
45+
}

0 commit comments

Comments
 (0)