13
0

move color utility functions from ARDOUR_UI_UTILS to Gtkmm2ext namespace (libs)

This commit is contained in:
Paul Davis 2022-08-10 21:11:46 -06:00
parent 32c2ed3f21
commit a2e4897a49
2 changed files with 61 additions and 0 deletions

View File

@ -25,11 +25,14 @@
#include <glib.h>
#include <gdkmm/color.h>
#include "pbd/failed_constructor.h"
#include "pbd/string_convert.h"
#include "gtkmm2ext/colors.h"
#include "gtkmm2ext/colorspace.h"
#include "gtkmm2ext/rgb_macros.h"
using namespace std;
using namespace Gtkmm2ext;
@ -736,3 +739,53 @@ Gtkmm2ext::random_color ()
{
return ((g_random_int() % 16777215) << 8 | 0xff);
}
Gdk::Color
Gtkmm2ext::gdk_color_from_rgb (uint32_t rgb)
{
Gdk::Color c;
set_color_from_rgb (c, rgb);
return c;
}
Gdk::Color
Gtkmm2ext::gdk_color_from_rgba (uint32_t rgba)
{
Gdk::Color c;
set_color_from_rgb (c, rgba >> 8);
return c;
}
void
Gtkmm2ext::set_color_from_rgb (Gdk::Color& c, uint32_t rgb)
{
/* Gdk::Color color ranges are 16 bit, so scale from 8 bit by
multiplying by 256.
*/
c.set_rgb ((rgb >> 16)*256, ((rgb & 0xff00) >> 8)*256, (rgb & 0xff)*256);
}
void
Gtkmm2ext::set_color_from_rgba (Gdk::Color& c, uint32_t rgba)
{
/* Gdk::Color color ranges are 16 bit, so scale from 8 bit by
multiplying by 256.
*/
c.set_rgb ((rgba >> 24)*256, ((rgba & 0xff0000) >> 16)*256, ((rgba & 0xff00) >> 8)*256);
}
uint32_t
Gtkmm2ext::gdk_color_to_rgba (Gdk::Color const& c)
{
/* since alpha value is not available from a Gdk::Color, it is
hardcoded as 0xff (aka 255 or 1.0)
*/
const uint32_t r = c.get_red_p () * 255.0;
const uint32_t g = c.get_green_p () * 255.0;
const uint32_t b = c.get_blue_p () * 255.0;
const uint32_t a = 0xff;
return RGBA_TO_UINT (r,g,b,a);
}

View File

@ -22,6 +22,7 @@
#include<stdint.h>
#include <gdkmm/types.h>
#include <cairomm/context.h>
#include "gtkmm2ext/visibility.h"
@ -40,6 +41,13 @@ extern LIBGTKMM2EXT_API void set_source_rgb_a (Cairo::RefPtr<Cairo::Context>, Gt
extern LIBGTKMM2EXT_API void set_source_rgba (cairo_t*, Gtkmm2ext::Color);
extern LIBGTKMM2EXT_API void set_source_rgb_a (cairo_t*, Gtkmm2ext::Color, float alpha); //override the color's alpha
extern LIBGTKMM2EXT_API Gdk::Color gdk_color_from_rgb (uint32_t);
extern LIBGTKMM2EXT_API Gdk::Color gdk_color_from_rgba (uint32_t);
extern LIBGTKMM2EXT_API uint32_t gdk_color_to_rgba (Gdk::Color const&);
extern LIBGTKMM2EXT_API void set_color_from_rgb (Gdk::Color&, uint32_t);
extern LIBGTKMM2EXT_API void set_color_from_rgba (Gdk::Color&, uint32_t);
struct LIBGTKMM2EXT_API HSV;
struct LIBGTKMM2EXT_API HSVA;