mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 17:47:36 +00:00
LibVT+Kernel: Create Color
class
Previously, we converted colors to their RGB values immediately when they were set. This meant that their semantic meaning was lost, we could not tell a precise RGB value apart from a named/indexed color. The new way of storing colors will allow us to retain this information, so we can change a color scheme on the fly, and previously emitted text will also be affected.
This commit is contained in:
parent
054c742d17
commit
99033876ec
7 changed files with 232 additions and 80 deletions
141
Userland/Libraries/LibVT/Color.h
Normal file
141
Userland/Libraries/LibVT/Color.h
Normal file
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Daniel Bertalan <dani@danielbertalan.dev>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace VT {
|
||||
|
||||
class Color {
|
||||
public:
|
||||
enum class ANSIColor : u16 {
|
||||
Black = 0,
|
||||
Red,
|
||||
Green,
|
||||
Yellow,
|
||||
Blue,
|
||||
Magenta,
|
||||
Cyan,
|
||||
White,
|
||||
BrightBlack,
|
||||
BrightRed,
|
||||
BrightGreen,
|
||||
BrightYellow,
|
||||
BrightBlue,
|
||||
BrightMagenta,
|
||||
BrightCyan,
|
||||
BrightWhite,
|
||||
// We use the values above to directly index into the color lookup table,
|
||||
// but the ones below are handled separately.
|
||||
DefaultForeground = 256,
|
||||
DefaultBackground
|
||||
};
|
||||
|
||||
static constexpr Color rgb(u32 rgb)
|
||||
{
|
||||
return Color(rgb);
|
||||
}
|
||||
|
||||
static constexpr Color indexed(u8 index)
|
||||
{
|
||||
return Color(index);
|
||||
}
|
||||
|
||||
static constexpr Color named(ANSIColor name)
|
||||
{
|
||||
return Color(name);
|
||||
}
|
||||
|
||||
constexpr bool is_rgb() const
|
||||
{
|
||||
return m_kind == Kind::RGB;
|
||||
}
|
||||
|
||||
constexpr bool is_indexed() const
|
||||
{
|
||||
return m_kind == Kind::Indexed;
|
||||
}
|
||||
|
||||
constexpr bool is_named() const
|
||||
{
|
||||
return m_kind == Kind::Named;
|
||||
}
|
||||
|
||||
constexpr u32 as_rgb() const
|
||||
{
|
||||
VERIFY(is_rgb());
|
||||
return m_value.as_rgb;
|
||||
}
|
||||
|
||||
constexpr u8 as_indexed() const
|
||||
{
|
||||
VERIFY(is_indexed());
|
||||
return m_value.as_indexed;
|
||||
}
|
||||
|
||||
constexpr ANSIColor as_named() const
|
||||
{
|
||||
VERIFY(is_named());
|
||||
return m_value.as_named;
|
||||
}
|
||||
|
||||
constexpr bool operator==(const Color& other) const
|
||||
{
|
||||
if (m_kind != other.kind())
|
||||
return false;
|
||||
|
||||
switch (m_kind) {
|
||||
case RGB:
|
||||
return m_value.as_rgb == other.as_rgb();
|
||||
case Indexed:
|
||||
return m_value.as_indexed == other.as_indexed();
|
||||
case Named:
|
||||
return m_value.as_named == other.as_named();
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
};
|
||||
}
|
||||
|
||||
enum Kind {
|
||||
RGB,
|
||||
Indexed,
|
||||
Named
|
||||
};
|
||||
|
||||
constexpr Kind kind() const
|
||||
{
|
||||
return m_kind;
|
||||
}
|
||||
|
||||
private:
|
||||
Kind m_kind;
|
||||
|
||||
union {
|
||||
u32 as_rgb;
|
||||
u8 as_indexed;
|
||||
ANSIColor as_named;
|
||||
} m_value;
|
||||
|
||||
constexpr Color(u32 rgb)
|
||||
: m_kind(Kind::RGB)
|
||||
{
|
||||
m_value.as_rgb = rgb;
|
||||
}
|
||||
|
||||
constexpr Color(u8 index)
|
||||
: m_kind(Kind::Indexed)
|
||||
{
|
||||
m_value.as_indexed = index;
|
||||
}
|
||||
|
||||
constexpr Color(ANSIColor name)
|
||||
: m_kind(Kind::Named)
|
||||
{
|
||||
m_value.as_named = name;
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue