2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-03 09:32:37 +00:00
|
|
|
#include <AK/Optional.h>
|
2019-12-23 19:24:26 +00:00
|
|
|
#include <AK/String.h>
|
2018-10-10 14:49:36 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2020-02-06 10:56:38 +00:00
|
|
|
namespace Gfx {
|
|
|
|
|
2019-12-24 19:57:54 +00:00
|
|
|
enum class ColorRole;
|
2019-07-03 19:17:35 +00:00
|
|
|
typedef u32 RGBA32;
|
2019-01-10 04:36:32 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
inline constexpr u32 make_rgb(u8 r, u8 g, u8 b)
|
2019-01-25 01:09:29 +00:00
|
|
|
{
|
|
|
|
return ((r << 16) | (g << 8) | b);
|
|
|
|
}
|
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
class Color {
|
|
|
|
public:
|
2019-06-07 15:13:23 +00:00
|
|
|
enum NamedColor {
|
2018-10-12 18:05:11 +00:00
|
|
|
Black,
|
|
|
|
White,
|
|
|
|
Red,
|
|
|
|
Green,
|
2019-05-05 22:59:33 +00:00
|
|
|
Cyan,
|
2018-10-12 18:05:11 +00:00
|
|
|
Blue,
|
2019-01-12 03:02:36 +00:00
|
|
|
Yellow,
|
|
|
|
Magenta,
|
2018-10-12 21:02:23 +00:00
|
|
|
DarkGray,
|
|
|
|
MidGray,
|
|
|
|
LightGray,
|
2019-06-30 07:23:16 +00:00
|
|
|
WarmGray,
|
2019-11-18 18:02:10 +00:00
|
|
|
DarkCyan,
|
2019-03-18 19:56:45 +00:00
|
|
|
DarkGreen,
|
|
|
|
DarkBlue,
|
|
|
|
DarkRed,
|
2019-11-18 18:02:10 +00:00
|
|
|
MidCyan,
|
2019-03-20 03:21:58 +00:00
|
|
|
MidGreen,
|
|
|
|
MidRed,
|
|
|
|
MidBlue,
|
|
|
|
MidMagenta,
|
2018-10-12 18:05:11 +00:00
|
|
|
};
|
|
|
|
|
2019-06-07 09:46:55 +00:00
|
|
|
Color() {}
|
2018-10-12 18:05:11 +00:00
|
|
|
Color(NamedColor);
|
2019-07-03 19:17:35 +00:00
|
|
|
Color(u8 r, u8 g, u8 b)
|
2019-06-07 09:46:55 +00:00
|
|
|
: m_value(0xff000000 | (r << 16) | (g << 8) | b)
|
|
|
|
{
|
|
|
|
}
|
2019-07-03 19:17:35 +00:00
|
|
|
Color(u8 r, u8 g, u8 b, u8 a)
|
2019-06-07 09:46:55 +00:00
|
|
|
: m_value((a << 24) | (r << 16) | (g << 8) | b)
|
|
|
|
{
|
|
|
|
}
|
2019-02-19 00:42:53 +00:00
|
|
|
|
|
|
|
static Color from_rgb(unsigned rgb) { return Color(rgb | 0xff000000); }
|
|
|
|
static Color from_rgba(unsigned rgba) { return Color(rgba); }
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
u8 red() const { return (m_value >> 16) & 0xff; }
|
|
|
|
u8 green() const { return (m_value >> 8) & 0xff; }
|
|
|
|
u8 blue() const { return m_value & 0xff; }
|
|
|
|
u8 alpha() const { return (m_value >> 24) & 0xff; }
|
2019-02-07 22:13:47 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
void set_alpha(u8 value)
|
2019-02-07 22:13:47 +00:00
|
|
|
{
|
2019-02-19 00:42:53 +00:00
|
|
|
m_value &= 0x00ffffff;
|
|
|
|
m_value |= value << 24;
|
2019-02-07 22:13:47 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
void set_red(u8 value)
|
2019-06-16 13:09:11 +00:00
|
|
|
{
|
|
|
|
m_value &= 0xff00ffff;
|
|
|
|
m_value |= value << 16;
|
|
|
|
}
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
void set_green(u8 value)
|
2019-06-16 13:09:11 +00:00
|
|
|
{
|
|
|
|
m_value &= 0xffff00ff;
|
|
|
|
m_value |= value << 8;
|
|
|
|
}
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
void set_blue(u8 value)
|
2019-06-16 13:09:11 +00:00
|
|
|
{
|
|
|
|
m_value &= 0xffffff00;
|
|
|
|
m_value |= value;
|
|
|
|
}
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
Color with_alpha(u8 alpha)
|
2019-02-19 00:42:53 +00:00
|
|
|
{
|
|
|
|
return Color((m_value & 0x00ffffff) | alpha << 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
Color blend(Color source) const
|
|
|
|
{
|
|
|
|
if (!alpha() || source.alpha() == 255)
|
|
|
|
return source;
|
|
|
|
|
|
|
|
if (!source.alpha())
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
|
2019-07-03 19:17:35 +00:00
|
|
|
u8 r = (red() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.red()) / d;
|
|
|
|
u8 g = (green() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.green()) / d;
|
|
|
|
u8 b = (blue() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.blue()) / d;
|
|
|
|
u8 a = d / 255;
|
2019-02-19 00:42:53 +00:00
|
|
|
return Color(r, g, b, a);
|
|
|
|
}
|
2019-01-25 04:01:27 +00:00
|
|
|
|
2019-04-10 14:00:29 +00:00
|
|
|
Color to_grayscale() const
|
|
|
|
{
|
|
|
|
int gray = (red() + green() + blue()) / 3;
|
|
|
|
return Color(gray, gray, gray, alpha());
|
|
|
|
}
|
|
|
|
|
2019-12-24 01:24:49 +00:00
|
|
|
Color darkened(float amount = 0.5f) const
|
2019-04-10 14:00:29 +00:00
|
|
|
{
|
2019-05-25 19:55:53 +00:00
|
|
|
return Color(red() * amount, green() * amount, blue() * amount, alpha());
|
2019-04-10 14:00:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 01:24:49 +00:00
|
|
|
Color lightened(float amount = 1.2f) const
|
2019-04-12 00:50:43 +00:00
|
|
|
{
|
2019-12-24 01:24:49 +00:00
|
|
|
return Color(min(255, (int)((float)red() * amount)), min(255, (int)((float)green() * amount)), min(255, (int)((float)blue() * amount)), alpha());
|
2019-04-12 00:50:43 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 05:28:59 +00:00
|
|
|
Color inverted() const
|
|
|
|
{
|
|
|
|
return Color(~red(), ~green(), ~blue());
|
|
|
|
}
|
|
|
|
|
2019-01-10 04:36:32 +00:00
|
|
|
RGBA32 value() const { return m_value; }
|
2018-10-10 14:49:36 +00:00
|
|
|
|
2019-06-14 17:10:43 +00:00
|
|
|
bool operator==(const Color& other) const
|
|
|
|
{
|
|
|
|
return m_value == other.m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Color& other) const
|
|
|
|
{
|
|
|
|
return m_value != other.m_value;
|
|
|
|
}
|
|
|
|
|
2019-03-18 03:53:09 +00:00
|
|
|
String to_string() const;
|
2019-08-03 09:32:37 +00:00
|
|
|
static Optional<Color> from_string(const StringView&);
|
2019-03-18 03:53:09 +00:00
|
|
|
|
2018-10-10 14:49:36 +00:00
|
|
|
private:
|
2019-06-07 09:46:55 +00:00
|
|
|
explicit Color(RGBA32 rgba)
|
|
|
|
: m_value(rgba)
|
|
|
|
{
|
|
|
|
}
|
2019-02-19 00:42:53 +00:00
|
|
|
|
2019-01-10 04:36:32 +00:00
|
|
|
RGBA32 m_value { 0 };
|
2018-10-10 14:49:36 +00:00
|
|
|
};
|
2019-07-23 12:56:17 +00:00
|
|
|
|
|
|
|
inline const LogStream& operator<<(const LogStream& stream, Color value)
|
|
|
|
{
|
|
|
|
return stream << value.to_string();
|
|
|
|
}
|
2020-02-06 10:56:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using Gfx::Color;
|