-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): allow colours in both rgb and hex
This commit introduces the ability for users to specify colours in the static config file in either RGB or Hex format. This is done by introducing a new enum, Colour, which provides variants wrapping the internal Rgb type, and the HexColour type from the hex_colour crate. HexColour itself is wrapped in a new struct, Hex, in order to allow the implementation of the JsonSchema trait. A number of From<T> implementations have been done in order to make working with the Colour enum more ergonomic. Ultimately, the core representation for colours in komorebi remains the Rgb struct, any and Hex-specified colours will be converted to this Rgb type before being converted to u32 to pass them on to various Win32 API calls.
- Loading branch information
Showing
7 changed files
with
245 additions
and
124 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use hex_color::HexColor; | ||
use schemars::gen::SchemaGenerator; | ||
use schemars::schema::InstanceType; | ||
use schemars::schema::Schema; | ||
use schemars::schema::SchemaObject; | ||
use schemars::JsonSchema; | ||
use serde::Deserialize; | ||
use serde::Serialize; | ||
|
||
#[derive(Debug, Copy, Clone, Serialize, Deserialize, JsonSchema)] | ||
#[serde(untagged)] | ||
pub enum Colour { | ||
/// Colour represented as RGB | ||
Rgb(Rgb), | ||
/// Colour represented as Hex | ||
Hex(Hex), | ||
} | ||
|
||
impl From<Rgb> for Colour { | ||
fn from(value: Rgb) -> Self { | ||
Self::Rgb(value) | ||
} | ||
} | ||
|
||
impl From<u32> for Colour { | ||
fn from(value: u32) -> Self { | ||
Self::Rgb(Rgb::from(value)) | ||
} | ||
} | ||
|
||
#[derive(Debug, Copy, Clone, Serialize, Deserialize)] | ||
pub struct Hex(HexColor); | ||
|
||
impl JsonSchema for Hex { | ||
fn schema_name() -> String { | ||
String::from("Hex") | ||
} | ||
|
||
fn json_schema(_: &mut SchemaGenerator) -> Schema { | ||
SchemaObject { | ||
instance_type: Some(InstanceType::String.into()), | ||
..Default::default() | ||
} | ||
.into() | ||
} | ||
} | ||
|
||
impl From<Colour> for u32 { | ||
fn from(value: Colour) -> Self { | ||
match value { | ||
Colour::Rgb(val) => val.into(), | ||
Colour::Hex(val) => (Rgb::from(val)).into(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Copy, Clone, Serialize, Deserialize, JsonSchema)] | ||
pub struct Rgb { | ||
/// Red | ||
pub r: u32, | ||
/// Green | ||
pub g: u32, | ||
/// Blue | ||
pub b: u32, | ||
} | ||
|
||
impl Rgb { | ||
pub fn new(r: u32, g: u32, b: u32) -> Self { | ||
Self { r, g, b } | ||
} | ||
} | ||
|
||
impl From<Hex> for Rgb { | ||
fn from(value: Hex) -> Self { | ||
value.0.into() | ||
} | ||
} | ||
|
||
impl From<HexColor> for Rgb { | ||
fn from(value: HexColor) -> Self { | ||
Self { | ||
r: value.r as u32, | ||
g: value.g as u32, | ||
b: value.b as u32, | ||
} | ||
} | ||
} | ||
|
||
impl From<Rgb> for u32 { | ||
fn from(value: Rgb) -> Self { | ||
value.r | (value.g << 8) | (value.b << 16) | ||
} | ||
} | ||
|
||
impl From<u32> for Rgb { | ||
fn from(value: u32) -> Self { | ||
Self { | ||
r: value & 0xff, | ||
g: value >> 8 & 0xff, | ||
b: value >> 16 & 0xff, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.