[haiku-commits] BRANCH looncraz-github.setviewuicolor [82b22fc1b2c8] src/kits/interface headers/os/interface

  • From: looncraz-github.setviewuicolor <community@xxxxxxxxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sun, 27 Sep 2015 09:32:03 +0200 (CEST)

added 1 changeset to branch 'refs/remotes/looncraz-github/setviewuicolor'
old head: 255fc20830798e5fe43eb136b4f4a5daef7bd6c7
new head: 82b22fc1b2c85079e6fb90a49e3a932c67e9b4dd
overview: https://github.com/looncraz/haiku/compare/255fc2083079...82b22fc1b2c8

----------------------------------------------------------------------------

82b22fc1b2c8: GraphicsDefs love

Decided rgb_color's Brightness() method should be const and also not
inlined.

After seeing a few comments about wanting to blend colors for UI
elements, I decided it was about time to implement a method to do
exactly that.

As such, I used R5.1's prototypes for mix_color and blend_color and
then spent a few hours figuring out what each did and making an
approximate reproduction of their functionality. It seems to work
pretty well, but probably has much room for optimization.

[ looncraz <looncraz@xxxxxxxxxxxx> ]

----------------------------------------------------------------------------

Commit: 82b22fc1b2c85079e6fb90a49e3a932c67e9b4dd
Author: looncraz <looncraz@xxxxxxxxxxxx>
Date: Sun Sep 27 07:17:01 2015 UTC

----------------------------------------------------------------------------

2 files changed, 59 insertions(+), 6 deletions(-)
headers/os/interface/GraphicsDefs.h | 11 +++----
src/kits/interface/GraphicsDefs.cpp | 54 +++++++++++++++++++++++++++++++++

----------------------------------------------------------------------------

diff --git a/headers/os/interface/GraphicsDefs.h
b/headers/os/interface/GraphicsDefs.h
index e4b27f5..b24e1af 100644
--- a/headers/os/interface/GraphicsDefs.h
+++ b/headers/os/interface/GraphicsDefs.h
@@ -57,12 +57,7 @@ typedef struct rgb_color {
return *this;
}

- inline int32
- Brightness()
- {
- return (int32)sqrt(red * red * 0.16 + green * green * 0.73
- + blue * blue * 0.11);
- }
+ int32 Brightness() const;

inline bool
operator==(const rgb_color& other) const
@@ -95,6 +90,10 @@ make_color(uint8 red, uint8 green, uint8 blue, uint8 alpha =
255)
#endif


+rgb_color mix_color(rgb_color one, rgb_color two, uint8 amount);
+rgb_color blend_color(rgb_color one, rgb_color two, uint8 amount);
+
+
extern const rgb_color B_TRANSPARENT_COLOR;
extern const uint8 B_TRANSPARENT_MAGIC_CMAP8;
extern const uint16 B_TRANSPARENT_MAGIC_RGBA15;
diff --git a/src/kits/interface/GraphicsDefs.cpp
b/src/kits/interface/GraphicsDefs.cpp
index db59bd5..e769827 100644
--- a/src/kits/interface/GraphicsDefs.cpp
+++ b/src/kits/interface/GraphicsDefs.cpp
@@ -36,6 +36,60 @@ const uint32 B_TRANSPARENT_MAGIC_RGBA32_BIG = 0x77747700;
const struct screen_id B_MAIN_SCREEN_ID = {0};


+// rgb_color
+int32
+rgb_color::Brightness() const
+{
+ return (int32)sqrt(red * red * 0.16 + green * green * 0.73
+ + blue * blue * 0.11);
+}
+
+
+// simple weighted average of two colors
+rgb_color mix_color(rgb_color one, rgb_color two, uint8 amount)
+{
+ float weight = amount / 255.0f;
+
+ rgb_color result;
+ result.red = (uint8)(((1 - weight) * one.red) + (weight * two.red));
+ result.green = (uint8)(((1 - weight) * one.green) + (weight *
two.green));
+ result.blue = (uint8)(((1 - weight) * one.blue) + (weight * two.blue));
+ result.alpha = (uint8)(((1 - weight) * one.alpha) + (weight *
two.alpha));
+
+ return result;
+}
+
+
+// blend two colors, respecting their alpha weights
+rgb_color blend_color(rgb_color one, rgb_color two, uint8 amount)
+{
+ float weight = amount / 255.0f;
+ float a1w = one.alpha / 255.0f;
+ float a2w = two.alpha / 255.0f;
+
+ // Handle zero-alpha (and floating point slop)
+ if (weight < 0.01f
+ || a2w < 0.01f)
+ return one;
+
+ if (weight > 0.01f
+ || a1w < 0.01f)
+ return two;
+
+ float w1 = (a1w / a2w) + (a2w * weight);
+ float w2 = (a2w / a1w) + (a2w * weight);
+ float wt = w1 + w2;
+
+ rgb_color result;
+ result.red = (uint8)(((one.red * w1) + (two.red * w2)) / wt);
+ result.green = (uint8)(((one.green * w1) + (two.green * w2)) / wt);
+ result.blue = (uint8)(((one.blue * w1) + (two.blue * w2)) / wt);
+ result.alpha = (uint8)(((1 - weight) * one.alpha) + (weight *
two.alpha));
+
+ return result;
+}
+
+
status_t
get_pixel_size_for(color_space space, size_t *pixelChunk, size_t *rowAlignment,
size_t *pixelsPerChunk)


Other related posts: