aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRay <raysan5@gmail.com>2018-02-12 11:55:22 +0100
committerRay <raysan5@gmail.com>2018-02-12 11:55:22 +0100
commit36750ffb9a71836d06cc2a951dfdf0e9ed87a61c (patch)
tree8ec3f76a6e9c45fcbafa4c68429946162bcd91fa /src
parent7530a60abcd02872cfd406117414adc22623619a (diff)
downloadraylib-36750ffb9a71836d06cc2a951dfdf0e9ed87a61c.tar.gz
raylib-36750ffb9a71836d06cc2a951dfdf0e9ed87a61c.zip
BREAKING CHANGE: Renamed function for consistency
Rename: GetHexValue() renamed to ColorToInt() Added: ColorToHSV()
Diffstat (limited to 'src')
-rw-r--r--src/core.c66
-rw-r--r--src/raylib.h5
2 files changed, 63 insertions, 8 deletions
diff --git a/src/core.c b/src/core.c
index ce017af2..e2fabcf6 100644
--- a/src/core.c
+++ b/src/core.c
@@ -1112,7 +1112,7 @@ double GetTime(void)
#endif
}
-// Converts Color to float array and normalizes
+// Returns normalized float array for a Color
float *ColorToFloat(Color color)
{
static float buffer[4];
@@ -1125,6 +1125,64 @@ float *ColorToFloat(Color color)
return buffer;
}
+// Returns hexadecimal value for a Color
+int ColorToInt(Color color)
+{
+ return (((int)color.r << 24) | ((int)color.g << 16) | ((int)color.b << 8) | (int)color.a);
+}
+
+// Returns HSV values for a Color
+// NOTE: Hue is returned as degrees [0..360]
+Vector3 ColorToHSV(Color color)
+{
+ Vector3 rgb = { (float)color.r/255.0f, (float)color.g/255.0f, (float)color.b/255.0f };
+ Vector3 hsv = { 0.0f, 0.0f, 0.0f };
+ float min, max, delta;
+
+ min = rgb.x < rgb.y ? rgb.x : rgb.y;
+ min = min < rgb.z ? min : rgb.z;
+
+ max = rgb.x > rgb.y ? rgb.x : rgb.y;
+ max = max > rgb.z ? max : rgb.z;
+
+ hsv.z = max; // Value
+ delta = max - min;
+
+ if (delta < 0.00001f)
+ {
+ hsv.y = 0.0f;
+ hsv.x = 0.0f; // Undefined, maybe NAN?
+ return hsv;
+ }
+
+ if (max > 0.0f)
+ {
+ // NOTE: If max is 0, this divide would cause a crash
+ hsv.y = (delta/max); // Saturation
+ }
+ else
+ {
+ // NOTE: If max is 0, then r = g = b = 0, s = 0, h is undefined
+ hsv.y = 0.0f;
+ hsv.x = NAN; // Undefined
+ return hsv;
+ }
+
+ // NOTE: Comparing float values could not work properly
+ if (rgb.x >= max) hsv.x = (rgb.y - rgb.z)/delta; // Between yellow & magenta
+ else
+ {
+ if (rgb.y >= max) hsv.x = 2.0f + (rgb.z - rgb.x)/delta; // Between cyan & yellow
+ else hsv.x = 4.0f + (rgb.x - rgb.y)/delta; // Between magenta & cyan
+ }
+
+ hsv.x *= 60.0f; // Convert to degrees
+
+ if (hsv.x < 0.0f) hsv.x += 360.0f;
+
+ return hsv;
+}
+
// Returns a Color struct from hexadecimal value
Color GetColor(int hexValue)
{
@@ -1138,11 +1196,7 @@ Color GetColor(int hexValue)
return color;
}
-// Returns hexadecimal value for a Color
-int GetHexValue(Color color)
-{
- return (((int)color.r << 24) | ((int)color.g << 16) | ((int)color.b << 8) | (int)color.a);
-}
+
// Returns a random value between min and max (both included)
int GetRandomValue(int min, int max)
diff --git a/src/raylib.h b/src/raylib.h
index e3f30e43..fba973d0 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -728,10 +728,11 @@ RLAPI float GetFrameTime(void); // Returns tim
RLAPI double GetTime(void); // Returns elapsed time in seconds since InitWindow()
// Color-related functions
-RLAPI int GetHexValue(Color color); // Returns hexadecimal value for a Color
+RLAPI float *ColorToFloat(Color color); // Returns normalized float array for a Color
+RLAPI int ColorToInt(Color color); // Returns hexadecimal value for a Color
+RLAPI Vector3 ColorToHSV(Color color); // Returns HSV values for a Color
RLAPI Color GetColor(int hexValue); // Returns a Color struct from hexadecimal value
RLAPI Color Fade(Color color, float alpha); // Color fade-in or fade-out, alpha goes from 0.0f to 1.0f
-RLAPI float *ColorToFloat(Color color); // Converts Color to float array and normalizes
// Math useful functions (available from raymath.h)
RLAPI float *Vector3ToFloat(Vector3 vec); // Returns Vector3 as float array