2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2009-07-28 21:44:22 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
2009-02-22 16:53:30 +00:00
|
|
|
*
|
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2021-02-27 15:14:19 +00:00
|
|
|
* Copyright (c) 2005-2021 Brenden Matthews, Philip Kovacs, et. al.
|
2009-02-22 16:53:30 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#ifndef _COLOURS_H
|
|
|
|
#define _COLOURS_H
|
|
|
|
|
2023-01-02 23:54:53 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <climits>
|
2023-02-24 13:43:15 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2023-01-02 23:54:53 +00:00
|
|
|
#ifdef BUILD_X11
|
|
|
|
#include "x11.h"
|
|
|
|
#endif /* BUILD_X11 */
|
2018-05-12 16:03:00 +00:00
|
|
|
|
2023-01-02 23:54:53 +00:00
|
|
|
struct Colour {
|
|
|
|
uint8_t red;
|
|
|
|
uint8_t green;
|
|
|
|
uint8_t blue;
|
|
|
|
uint8_t alpha;
|
|
|
|
|
2023-02-24 13:43:15 +00:00
|
|
|
public:
|
2023-02-24 13:13:19 +00:00
|
|
|
// Compare two instances.
|
|
|
|
bool operator==(const Colour &c) const {
|
|
|
|
return c.red == red && c.green == green && c.blue == blue &&
|
|
|
|
c.alpha == alpha;
|
|
|
|
}
|
|
|
|
|
2023-01-02 23:54:53 +00:00
|
|
|
// Express the color as a 32-bit ARGB integer (alpha in MSB).
|
|
|
|
uint32_t to_argb32(void) {
|
|
|
|
uint32_t out;
|
|
|
|
out = alpha << 24 | red << 16 | green << 8 | blue;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct from a 32-bit ARGB integer (alpha in MSB).
|
|
|
|
static Colour from_argb32(uint32_t argb);
|
|
|
|
|
|
|
|
#ifdef BUILD_X11
|
2023-02-24 13:13:48 +00:00
|
|
|
unsigned long to_x11_color(Display *display, int screen,
|
|
|
|
bool premultiply = false) {
|
|
|
|
if (display == nullptr) {
|
2023-01-02 23:54:53 +00:00
|
|
|
/* cannot work if display is not open */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-24 13:13:48 +00:00
|
|
|
XColor xcolor{};
|
2023-01-02 23:54:53 +00:00
|
|
|
xcolor.red = red * 257;
|
|
|
|
xcolor.green = green * 257;
|
|
|
|
xcolor.blue = blue * 257;
|
|
|
|
if (XAllocColor(display, DefaultColormap(display, screen), &xcolor) == 0) {
|
2023-02-24 13:13:48 +00:00
|
|
|
// NORM_ERR("can't allocate X color");
|
2023-01-02 23:54:53 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-24 13:13:48 +00:00
|
|
|
unsigned long pixel = static_cast<unsigned long>(xcolor.pixel) & 0xffffff;
|
|
|
|
#ifdef BUILD_ARGB
|
|
|
|
if (have_argb_visual) {
|
|
|
|
if (premultiply)
|
|
|
|
pixel = (red * alpha / 255) << 16 | (green * alpha / 255) << 8 |
|
|
|
|
(blue * alpha / 255);
|
|
|
|
pixel |= ((unsigned long)alpha << 24);
|
|
|
|
}
|
|
|
|
#endif /* BUILD_ARGB */
|
|
|
|
return pixel;
|
2023-01-02 23:54:53 +00:00
|
|
|
}
|
|
|
|
#endif /* BUILD_X11 */
|
|
|
|
};
|
|
|
|
|
2023-02-05 05:47:06 +00:00
|
|
|
extern Colour error_colour;
|
|
|
|
|
2023-01-05 20:54:30 +00:00
|
|
|
Colour parse_color(const std::string &colour);
|
2010-02-28 11:26:18 +00:00
|
|
|
// XXX: when everyone uses C++ strings, remove this C version
|
2023-01-05 20:54:30 +00:00
|
|
|
Colour parse_color(const char *);
|
2010-01-04 04:50:02 +00:00
|
|
|
|
2009-02-22 16:53:30 +00:00
|
|
|
#endif /* _COLOURS_H */
|