mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-10 13:00:29 +03:00
94ca55cefd
As suggested by Joshua, this commit adds the 2-clause BSD license as a comment block to the top of every source file. For the first pass, I've just added myself for simplicity. I encourage everyone to add themselves as copyright holders of any file they've added or modified in some significant way. If I've added myself in error somewhere, feel free to replace it with the appropriate copyright holder instead. Going forward, all new source files should include a license header.
180 lines
4.8 KiB
C++
180 lines
4.8 KiB
C++
/*
|
|
* 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.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Optional.h>
|
|
#include <AK/String.h>
|
|
#include <AK/Types.h>
|
|
|
|
enum class ColorRole;
|
|
typedef u32 RGBA32;
|
|
|
|
inline constexpr u32 make_rgb(u8 r, u8 g, u8 b)
|
|
{
|
|
return ((r << 16) | (g << 8) | b);
|
|
}
|
|
|
|
class Color {
|
|
public:
|
|
enum NamedColor {
|
|
Black,
|
|
White,
|
|
Red,
|
|
Green,
|
|
Cyan,
|
|
Blue,
|
|
Yellow,
|
|
Magenta,
|
|
DarkGray,
|
|
MidGray,
|
|
LightGray,
|
|
WarmGray,
|
|
DarkCyan,
|
|
DarkGreen,
|
|
DarkBlue,
|
|
DarkRed,
|
|
MidCyan,
|
|
MidGreen,
|
|
MidRed,
|
|
MidBlue,
|
|
MidMagenta,
|
|
};
|
|
|
|
Color() {}
|
|
Color(NamedColor);
|
|
Color(u8 r, u8 g, u8 b)
|
|
: m_value(0xff000000 | (r << 16) | (g << 8) | b)
|
|
{
|
|
}
|
|
Color(u8 r, u8 g, u8 b, u8 a)
|
|
: m_value((a << 24) | (r << 16) | (g << 8) | b)
|
|
{
|
|
}
|
|
|
|
static Color from_rgb(unsigned rgb) { return Color(rgb | 0xff000000); }
|
|
static Color from_rgba(unsigned rgba) { return Color(rgba); }
|
|
|
|
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; }
|
|
|
|
void set_alpha(u8 value)
|
|
{
|
|
m_value &= 0x00ffffff;
|
|
m_value |= value << 24;
|
|
}
|
|
|
|
void set_red(u8 value)
|
|
{
|
|
m_value &= 0xff00ffff;
|
|
m_value |= value << 16;
|
|
}
|
|
|
|
void set_green(u8 value)
|
|
{
|
|
m_value &= 0xffff00ff;
|
|
m_value |= value << 8;
|
|
}
|
|
|
|
void set_blue(u8 value)
|
|
{
|
|
m_value &= 0xffffff00;
|
|
m_value |= value;
|
|
}
|
|
|
|
Color with_alpha(u8 alpha)
|
|
{
|
|
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();
|
|
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;
|
|
return Color(r, g, b, a);
|
|
}
|
|
|
|
Color to_grayscale() const
|
|
{
|
|
int gray = (red() + green() + blue()) / 3;
|
|
return Color(gray, gray, gray, alpha());
|
|
}
|
|
|
|
Color darkened(float amount = 0.5f) const
|
|
{
|
|
return Color(red() * amount, green() * amount, blue() * amount, alpha());
|
|
}
|
|
|
|
Color lightened(float amount = 1.2f) const
|
|
{
|
|
return Color(min(255, (int)((float)red() * amount)), min(255, (int)((float)green() * amount)), min(255, (int)((float)blue() * amount)), alpha());
|
|
}
|
|
|
|
Color inverted() const
|
|
{
|
|
return Color(~red(), ~green(), ~blue());
|
|
}
|
|
|
|
RGBA32 value() const { return m_value; }
|
|
|
|
bool operator==(const Color& other) const
|
|
{
|
|
return m_value == other.m_value;
|
|
}
|
|
|
|
bool operator!=(const Color& other) const
|
|
{
|
|
return m_value != other.m_value;
|
|
}
|
|
|
|
String to_string() const;
|
|
static Optional<Color> from_string(const StringView&);
|
|
|
|
private:
|
|
explicit Color(RGBA32 rgba)
|
|
: m_value(rgba)
|
|
{
|
|
}
|
|
|
|
RGBA32 m_value { 0 };
|
|
};
|
|
|
|
inline const LogStream& operator<<(const LogStream& stream, Color value)
|
|
{
|
|
return stream << value.to_string();
|
|
}
|