Skip to content

Commit 264cc69

Browse files
authored
Merge pull request #620 from scratchcpp/color_api
Add color API
2 parents acff91c + a6069ae commit 264cc69

20 files changed

+246
-54
lines changed

include/scratchcpp/ispritehandler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ class LIBSCRATCHCPP_EXPORT ISpriteHandler
8585
virtual bool touchingPoint(double x, double y) const = 0;
8686

8787
/*! Used to check whether the sprite touches the given color. */
88-
virtual bool touchingColor(const Value &color) const = 0;
88+
virtual bool touchingColor(Rgb color) const = 0;
8989

9090
/*! Used to check whether the mask part of the sprite touches the given color. */
91-
virtual bool touchingColor(const Value &color, const Value &mask) const = 0;
91+
virtual bool touchingColor(Rgb color, Rgb mask) const = 0;
9292
};
9393

9494
} // namespace libscratchcpp

include/scratchcpp/istagehandler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ class LIBSCRATCHCPP_EXPORT IStageHandler
6262
virtual bool touchingPoint(double x, double y) const = 0;
6363

6464
/*! Used to check whether the stage touches the given color. */
65-
virtual bool touchingColor(const Value &color) const = 0;
65+
virtual bool touchingColor(Rgb color) const = 0;
6666

6767
/*! Used to check whether the mask part of the stage touches the given color. */
68-
virtual bool touchingColor(const Value &color, const Value &mask) const = 0;
68+
virtual bool touchingColor(Rgb color, Rgb mask) const = 0;
6969
};
7070

7171
} // namespace libscratchcpp

include/scratchcpp/sprite.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ class LIBSCRATCHCPP_EXPORT Sprite
8181
void keepInFence(double newX, double newY, double *fencedX, double *fencedY) const;
8282

8383
bool touchingPoint(double x, double y) const override;
84-
bool touchingColor(const Value &color) const override;
85-
bool touchingColor(const Value &color, const Value &mask) const override;
84+
bool touchingColor(Rgb color) const override;
85+
bool touchingColor(Rgb color, Rgb mask) const override;
8686

8787
void setGraphicsEffectValue(IGraphicsEffect *effect, double value) override;
8888

include/scratchcpp/stage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ class LIBSCRATCHCPP_EXPORT Stage : public Target
5555
Rect fastBoundingRect() const override;
5656

5757
bool touchingPoint(double x, double y) const override;
58-
bool touchingColor(const Value &color) const override;
59-
bool touchingColor(const Value &color, const Value &mask) const override;
58+
bool touchingColor(Rgb color) const override;
59+
bool touchingColor(Rgb color, Rgb mask) const override;
6060

6161
void setGraphicsEffectValue(IGraphicsEffect *effect, double value) override;
6262

include/scratchcpp/target.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include <vector>
6+
#include <scratchcpp/value_functions.h>
67

78
#include "drawable.h"
89
#include "rect.h"
@@ -12,7 +13,6 @@ namespace libscratchcpp
1213
{
1314

1415
class Variable;
15-
struct ValueData;
1616
class List;
1717
class Block;
1818
class Comment;
@@ -97,8 +97,8 @@ class LIBSCRATCHCPP_EXPORT Target : public Drawable
9797
bool touchingSprite(Sprite *sprite) const;
9898
virtual bool touchingPoint(double x, double y) const;
9999
bool touchingEdge() const;
100-
virtual bool touchingColor(const Value &color) const;
101-
virtual bool touchingColor(const Value &color, const Value &mask) const;
100+
virtual bool touchingColor(Rgb color) const;
101+
virtual bool touchingColor(Rgb color, Rgb mask) const;
102102

103103
double graphicsEffectValue(IGraphicsEffect *effect) const;
104104
virtual void setGraphicsEffectValue(IGraphicsEffect *effect, double value);

include/scratchcpp/value.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ class LIBSCRATCHCPP_EXPORT Value
139139
return ret;
140140
}
141141

142+
/*! Converts the value to an RGBA quadruplet. */
143+
Rgb toRgba() const { return value_toRgba(&m_data); }
144+
142145
/*! Adds the given value to the value. */
143146
void add(const Value &v) { value_add(&m_data, &v.m_data, &m_data); }
144147

include/scratchcpp/value_functions.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,45 @@
55
namespace libscratchcpp
66
{
77

8+
/*! A typedef for unsigned int. Holds the RGBA values. */
9+
using Rgb = unsigned int;
10+
11+
/*! Returns the red component of the quadruplet rgb. */
12+
inline constexpr int red(Rgb rgb)
13+
{
14+
return ((rgb >> 16) & 0xff);
15+
}
16+
17+
/*! Returns the green component of the quadruplet rgb. */
18+
inline constexpr int green(Rgb rgb)
19+
{
20+
return ((rgb >> 8) & 0xff);
21+
}
22+
23+
/*! Returns the blue component of the quadruplet rgb. */
24+
inline constexpr int blue(Rgb rgb)
25+
{
26+
return (rgb & 0xff);
27+
}
28+
29+
/*! Returns the alpha component of the quadruplet rgb. */
30+
inline constexpr int alpha(Rgb rgb)
31+
{
32+
return rgb >> 24;
33+
}
34+
35+
/*! Creates an RGB triplet from the given color components. */
36+
inline constexpr Rgb rgb(int r, int g, int b)
37+
{
38+
return (0xffu << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu);
39+
}
40+
41+
/*! Creates an RGBA quadruplet from the given color components. */
42+
inline constexpr Rgb rgba(int r, int g, int b, int a)
43+
{
44+
return ((a & 0xffu) << 24) | ((r & 0xffu) << 16) | ((g & 0xffu) << 8) | (b & 0xffu);
45+
}
46+
847
extern "C"
948
{
1049
LIBSCRATCHCPP_EXPORT void value_free(ValueData *v);
@@ -33,6 +72,7 @@ extern "C"
3372
LIBSCRATCHCPP_EXPORT void value_toString(const ValueData *v, std::string *dst);
3473
LIBSCRATCHCPP_EXPORT char *value_toCString(const ValueData *v);
3574
LIBSCRATCHCPP_EXPORT void value_toUtf16(const ValueData *v, std::u16string *dst);
75+
LIBSCRATCHCPP_EXPORT Rgb value_toRgba(const ValueData *v);
3676

3777
LIBSCRATCHCPP_EXPORT bool value_doubleIsInt(double v);
3878

src/blocks/sensingblocks.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,13 @@ unsigned int SensingBlocks::touchingEdge(VirtualMachine *vm)
538538

539539
unsigned int SensingBlocks::touchingColor(VirtualMachine *vm)
540540
{
541-
vm->replaceReturnValue(vm->target()->touchingColor(*vm->getInput(0, 1)), 1);
541+
vm->replaceReturnValue(vm->target()->touchingColor(vm->getInput(0, 1)->toRgba()), 1);
542542
return 0;
543543
}
544544

545545
unsigned int SensingBlocks::colorIsTouchingColor(VirtualMachine *vm)
546546
{
547-
vm->replaceReturnValue(vm->target()->touchingColor(*vm->getInput(0, 2), *vm->getInput(1, 2)), 2);
547+
vm->replaceReturnValue(vm->target()->touchingColor(vm->getInput(0, 2)->toRgba(), vm->getInput(1, 2)->toRgba()), 2);
548548
return 1;
549549
}
550550

src/scratch/sprite.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ bool Sprite::touchingPoint(double x, double y) const
504504
}
505505

506506
/*! Overrides Target#touchingColor(). */
507-
bool Sprite::touchingColor(const Value &color) const
507+
bool Sprite::touchingColor(Rgb color) const
508508
{
509509
if (!impl->iface)
510510
return false;
@@ -513,7 +513,7 @@ bool Sprite::touchingColor(const Value &color) const
513513
}
514514

515515
/*! Overrides Target#touchingColor(). */
516-
bool Sprite::touchingColor(const Value &color, const Value &mask) const
516+
bool Sprite::touchingColor(Rgb color, Rgb mask) const
517517
{
518518
if (!impl->iface)
519519
return false;

src/scratch/stage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ bool Stage::touchingPoint(double x, double y) const
186186
}
187187

188188
/*! Overrides Target#touchingColor(). */
189-
bool Stage::touchingColor(const Value &color) const
189+
bool Stage::touchingColor(Rgb color) const
190190
{
191191
if (!impl->iface)
192192
return false;
@@ -195,7 +195,7 @@ bool Stage::touchingColor(const Value &color) const
195195
}
196196

197197
/*! Overrides Target#touchingColor(). */
198-
bool Stage::touchingColor(const Value &color, const Value &mask) const
198+
bool Stage::touchingColor(Rgb color, Rgb mask) const
199199
{
200200
if (!impl->iface)
201201
return false;

0 commit comments

Comments
 (0)