Skip to content

Commit c9aa5fc

Browse files
committed
Move PenState out from pen blocks
1 parent cf2112b commit c9aa5fc

File tree

7 files changed

+84
-18
lines changed

7 files changed

+84
-18
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ qt_add_qml_module(scratchcpp-render
5555
penlayerpainter.cpp
5656
penlayerpainter.h
5757
penattributes.h
58+
penstate.h
5859
blocks/penextension.cpp
5960
blocks/penextension.h
6061
blocks/penblocks.cpp

src/blocks/penblocks.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "penblocks.h"
77
#include "penlayer.h"
8+
#include "penstate.h"
89
#include "spritemodel.h"
910

1011
using namespace scratchcpprender;
@@ -188,7 +189,8 @@ SpriteModel *PenBlocks::getSpriteModel(libscratchcpp::VirtualMachine *vm)
188189

189190
void PenBlocks::setOrChangeColorParam(PenAttributes &penAttributes, ColorParam param, double value, bool change)
190191
{
191-
PenState penState(penAttributes.color);
192+
PenState penState;
193+
penState.setColor(penAttributes.color);
192194

193195
switch (param) {
194196
case ColorParam::COLOR:

src/blocks/penblocks.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,6 @@ class PenBlocks : public libscratchcpp::IBlockSection
4242
static unsigned int changePenHueBy(libscratchcpp::VirtualMachine *vm);
4343

4444
private:
45-
struct PenState
46-
{
47-
PenState(const QColor &color)
48-
{
49-
QColor hsvColor = color.toHsv();
50-
this->color = hsvColor.hue() * 100 / 360.0;
51-
this->saturation = hsvColor.saturationF() * 100;
52-
this->brightness = hsvColor.valueF() * 100;
53-
this->transparency = 100 * (1 - hsvColor.alphaF());
54-
}
55-
56-
double color = 0;
57-
double saturation = 0;
58-
double brightness = 0;
59-
double transparency = 0;
60-
};
61-
6245
enum class ColorParam
6346
{
6447
COLOR

src/penstate.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#pragma once
4+
5+
#include <QColor>
6+
7+
#include "penattributes.h"
8+
9+
namespace scratchcpprender
10+
{
11+
12+
struct PenState
13+
{
14+
bool penDown = false;
15+
double color = 66.66;
16+
double saturation = 100;
17+
double brightness = 100;
18+
double transparency = 0;
19+
double shade = 50; // for legacy blocks
20+
PenAttributes penAttributes;
21+
22+
void setColor(const QColor &color)
23+
{
24+
QColor hsvColor = color.toHsv();
25+
this->color = hsvColor.hue() * 100 / 360.0;
26+
this->saturation = hsvColor.saturationF() * 100;
27+
this->brightness = hsvColor.valueF() * 100;
28+
this->transparency = 100 * (1 - hsvColor.alphaF());
29+
}
30+
};
31+
32+
} // namespace scratchcpprender

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ add_subdirectory(monitor_models)
3232
add_subdirectory(texture)
3333
add_subdirectory(skins)
3434
add_subdirectory(penattributes)
35+
add_subdirectory(penstate)
3536
add_subdirectory(penlayer)
3637
add_subdirectory(penlayerpainter)
3738
add_subdirectory(blocks)

test/penstate/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_executable(
2+
penstate_test
3+
penstate_test.cpp
4+
)
5+
6+
target_link_libraries(
7+
penstate_test
8+
GTest::gtest_main
9+
scratchcpp-render
10+
qnanopainter
11+
)
12+
13+
add_test(penstate_test)
14+
gtest_discover_tests(penstate_test)

test/penstate/penstate_test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <penstate.h>
2+
3+
#include "../common.h"
4+
5+
using namespace scratchcpprender;
6+
7+
TEST(PenStateTest, DefaultPenState)
8+
{
9+
PenState state;
10+
ASSERT_FALSE(state.penDown);
11+
ASSERT_EQ(state.color, 66.66);
12+
ASSERT_EQ(state.saturation, 100);
13+
ASSERT_EQ(state.brightness, 100);
14+
ASSERT_EQ(state.transparency, 0);
15+
ASSERT_EQ(state.shade, 50);
16+
17+
PenAttributes defaultAttributes;
18+
ASSERT_EQ(state.penAttributes.color, defaultAttributes.color);
19+
ASSERT_EQ(state.penAttributes.diameter, defaultAttributes.diameter);
20+
}
21+
22+
TEST(PenStateTest, SetColor)
23+
{
24+
PenState state;
25+
QColor color(64, 20, 189, 167);
26+
state.setColor(color);
27+
ASSERT_FALSE(state.penDown);
28+
ASSERT_EQ(std::round(state.color * 100) / 100, 70.83);
29+
ASSERT_EQ(std::round(state.saturation * 100) / 100, 89.42);
30+
ASSERT_EQ(std::round(state.brightness * 100) / 100, 74.12);
31+
ASSERT_EQ(std::round(state.transparency * 100) / 100, 34.51);
32+
ASSERT_EQ(state.shade, 50);
33+
}

0 commit comments

Comments
 (0)