Permalink
Browse files

UI: Add colorBlend() helper.

  • Loading branch information...
unknownbrackets committed Dec 3, 2017
1 parent aa0cc67 commit 7ab3e58deccc3efc6ff3a26ed8be74b1689231b4
Showing with 24 additions and 11 deletions.
  1. +24 −11 ext/native/base/colorutil.cpp
@@ -1,5 +1,14 @@
#include "base/colorutil.h"
template <typename T>
static T clamp(T f, T low, T high) {
if (f < low)
return low;
if (f > high)
return high;
return f;
}
uint32_t whiteAlpha(float alpha) {
if (alpha < 0.0f) alpha = 0.0f;
if (alpha > 1.0f) alpha = 1.0f;
@@ -20,6 +29,20 @@ uint32_t colorAlpha(uint32_t rgb, float alpha) {
return ((int)(alpha*255)<<24) | (rgb & 0xFFFFFF);
}
uint32_t colorBlend(uint32_t rgb1, uint32_t rgb2, float alpha) {
float invAlpha = (1.0f - alpha);
int r = (int)(((rgb1 >> 0) & 0xFF) * alpha + ((rgb2 >> 0) & 0xFF) * invAlpha);
int g = (int)(((rgb1 >> 8) & 0xFF) * alpha + ((rgb2 >> 8) & 0xFF) * invAlpha);
int b = (int)(((rgb1 >> 16) & 0xFF) * alpha + ((rgb2 >> 16) & 0xFF) * invAlpha);
int a = (int)(((rgb1 >> 24) & 0xFF) * alpha + ((rgb2 >> 24) & 0xFF) * invAlpha);
uint32_t c = clamp(a, 0, 255) << 24;
c |= clamp(b, 0, 255) << 16;
c |= clamp(g, 0, 255) << 8;
c |= clamp(r, 0, 255);
return c;
}
uint32_t alphaMul(uint32_t color, float alphaMul) {
uint32_t rgb = color & 0xFFFFFF;
int32_t alpha = color >> 24;
@@ -38,17 +61,7 @@ uint32_t rgba(float r, float g, float b, float alpha) {
}
uint32_t rgba_clamp(float r, float g, float b, float a) {
if (r > 1.0f) r = 1.0f;
if (g > 1.0f) g = 1.0f;
if (b > 1.0f) b = 1.0f;
if (a > 1.0f) a = 1.0f;
if (r < 0.0f) r = 0.0f;
if (g < 0.0f) g = 0.0f;
if (b < 0.0f) b = 0.0f;
if (a < 0.0f) a = 0.0f;
return rgba(r,g,b,a);
return rgba(clamp(r, 0.0f, 1.0f), clamp(g, 0.0f, 1.0f), clamp(b, 0.0f, 1.0f), clamp(a, 0.0f, 1.0f));
}
/* hsv2rgb.c

0 comments on commit 7ab3e58

Please sign in to comment.