浏览代码

Merge pull request #2184 from hsnabn/color-fromuint

Add Color::FromUInt.
Eugene Kozlov 8 年之前
父节点
当前提交
e9c72fac33

+ 1 - 0
Source/Urho3D/AngelScript/MathAPI.cpp

@@ -1388,6 +1388,7 @@ static void RegisterColor(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Color", "uint ToUInt() const", asMETHOD(Color, ToUInt), asCALL_THISCALL);
     engine->RegisterObjectMethod("Color", "Vector3 ToHSL() const", asMETHOD(Color, ToHSL), asCALL_THISCALL);
     engine->RegisterObjectMethod("Color", "Vector3 ToHSV() const", asMETHOD(Color, ToHSV), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Color", "void FromUInt(uint)", asMETHOD(Color, FromUInt), asCALL_THISCALL);
     engine->RegisterObjectMethod("Color", "void FromHSL(float, float, float, float)", asMETHOD(Color, FromHSL), asCALL_THISCALL);
     engine->RegisterObjectMethod("Color", "void FromHSV(float, float, float, float)", asMETHOD(Color, FromHSV), asCALL_THISCALL);
     engine->RegisterObjectMethod("Color", "Vector3 get_rgb() const", asMETHOD(Color, ToVector3), asCALL_THISCALL);

+ 1 - 0
Source/Urho3D/LuaScript/pkgs/Math/Color.pkg

@@ -16,6 +16,7 @@ class Color
     unsigned ToUInt() const;
     Vector3 ToHSL() const;
     Vector3 ToHSV() const;
+    void FromUInt(unsigned color);
     void FromHSL(float h, float s, float l, float a);
     void FromHSV(float h, float s, float v, float a);
 

+ 8 - 0
Source/Urho3D/Math/Color.cpp

@@ -64,6 +64,14 @@ Vector3 Color::ToHSV() const
     return Vector3(h, s, v);
 }
 
+void Color::FromUInt(unsigned color)
+{
+    a_ = (float)(((color >> 24) & 0xff) / 255.0f);
+    b_ = (float)(((color >> 16) & 0xff) / 255.0f);
+    g_ = (float)(((color >> 8)  & 0xff) / 255.0f);
+    r_ = (float)(((color >> 0)  & 0xff) / 255.0f);
+}
+
 void Color::FromHSL(float h, float s, float l, float a)
 {
     float c;

+ 2 - 0
Source/Urho3D/Math/Color.h

@@ -137,6 +137,8 @@ public:
     Vector3 ToHSL() const;
     /// Return HSV color-space representation as a Vector3; the RGB values are clipped before conversion but not changed in the process.
     Vector3 ToHSV() const;
+    /// Set RGBA values from packed 32-bit integer, with R component in the lowest 8 bits (format 0xAABBGGRR).
+    void FromUInt(unsigned color);
     /// Set RGBA values from specified HSL values and alpha.
     void FromHSL(float h, float s, float l, float a = 1.0f);
     /// Set RGBA values from specified HSV values and alpha.