Skip to content

Commit

Permalink
Merge pull request #36379 from aaronfranke/color-constructors
Browse files Browse the repository at this point in the history
Add a Color constructor for Color with alpha
  • Loading branch information
akien-mga authored May 7, 2020
2 parents bef5295 + 90df1d6 commit ae33cf5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
12 changes: 11 additions & 1 deletion core/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ struct Color {
operator String() const;

/**
* No construct parameters, r=0, g=0, b=0. a=255
* No construct parameters, r=0, g=0, b=0. a=1
*/
_FORCE_INLINE_ Color() {
r = 0;
Expand All @@ -223,6 +223,16 @@ struct Color {
b = p_b;
a = p_a;
}

/**
* Construct a Color from another Color, but with the specified alpha value.
*/
_FORCE_INLINE_ Color(const Color &p_c, float p_a) {
r = p_c.r;
g = p_c.g;
b = p_c.b;
a = p_a;
}
};

bool Color::operator<(const Color &p_color) const {
Expand Down
5 changes: 0 additions & 5 deletions core/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ String Variant::get_type_name(Variant::Type p_type) {
return "Plane";

} break;
/*
case QUAT: {
} break;*/
case AABB: {

return "AABB";
Expand Down
7 changes: 7 additions & 0 deletions core/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,11 @@ struct _VariantCall {
r_ret = Color::hex(*p_args[0]);
}

static void Color_init5(Variant &r_ret, const Variant **p_args) {

r_ret = Color(((Color)(*p_args[0])), *p_args[1]);
}

static void AABB_init1(Variant &r_ret, const Variant **p_args) {

r_ret = ::AABB(*p_args[0], *p_args[1]);
Expand Down Expand Up @@ -2211,6 +2216,8 @@ void register_variant_methods() {

_VariantCall::add_constructor(_VariantCall::Color_init1, Variant::COLOR, "r", Variant::FLOAT, "g", Variant::FLOAT, "b", Variant::FLOAT, "a", Variant::FLOAT);
_VariantCall::add_constructor(_VariantCall::Color_init2, Variant::COLOR, "r", Variant::FLOAT, "g", Variant::FLOAT, "b", Variant::FLOAT);
// init3 and init4 are the constructors for HTML hex strings and integers respectively which don't need binding here, so we skip to init5.
_VariantCall::add_constructor(_VariantCall::Color_init5, Variant::COLOR, "c", Variant::COLOR, "a", Variant::FLOAT);

_VariantCall::add_constructor(_VariantCall::AABB_init1, Variant::AABB, "position", Variant::VECTOR3, "size", Variant::VECTOR3);

Expand Down
14 changes: 14 additions & 0 deletions doc/classes/Color.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@
[/codeblock]
</description>
</method>
<method name="Color">
<return type="Color">
</return>
<argument index="0" name="c" type="Color">
</argument>
<argument index="1" name="a" type="float">
</argument>
<description>
Constructs a color from an existing color, but with a custom alpha value.
[codeblock]
var red = Color(Color.red, 0.5) # 50% transparent red.
[/codeblock]
</description>
</method>
<method name="Color">
<return type="Color">
</return>
Expand Down
10 changes: 9 additions & 1 deletion modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ public string ToHtml(bool includeAlpha = true)
return txt;
}

// Constructors
// Constructors
public Color(float r, float g, float b, float a = 1.0f)
{
this.r = r;
Expand All @@ -429,6 +429,14 @@ public Color(float r, float g, float b, float a = 1.0f)
this.a = a;
}

public Color(Color c, float a = 1.0f)
{
r = c.r;
g = c.g;
b = c.b;
this.a = a;
}

public Color(uint rgba)
{
a = (rgba & 0xFF) / 255.0f;
Expand Down

0 comments on commit ae33cf5

Please sign in to comment.