Skip to content

Commit

Permalink
Simplify parser daemon formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
eero-lehtinen committed Oct 27, 2024
1 parent b395000 commit f954b8b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ fn raw_alpha_u8(alpha: u8, use_alpha: bool) -> String {
}
}

/// Hex with no alpha, always long form
pub fn format_normalized_hex_no_alpha(color: Srgba) -> String {
let [r, g, b, _] = color.to_u8_array();
format!("#{:02x}{:02x}{:02x}", r, g, b)
}

pub fn format_color(fallback: LinearRgba, format: ColorFormat, use_alpha: bool) -> String {
match format {
ColorFormat::Css(format) => match format {
Expand Down
19 changes: 12 additions & 7 deletions src/parser_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use interprocess::local_socket::{
use std::{fs, io, process::ExitCode};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};

use crate::cli::CliColorFormat;
use crate::formats::{self, CssColorFormat};
use crate::{cli::CliColorFormat, formats, gamut};

const SOCKET_NAME: &str = concat!(env!("CARGO_PKG_NAME"), ".sock");

Expand Down Expand Up @@ -115,12 +114,12 @@ pub fn start() -> ExitCode {
})
}

fn handle_message(srt: &str) -> anyhow::Result<String> {
if srt == "test" {
fn handle_message(msg: &str) -> anyhow::Result<String> {
if msg == "test" {
bail!("test");
}

let (number, rest) = srt
let (number, rest) = msg
.split_once(":")
.context("Read didn't contain the ':' delimiter !")?;

Expand All @@ -129,8 +128,14 @@ fn handle_message(srt: &str) -> anyhow::Result<String> {
.split_once(";")
.context("Read didn't contain the ';' delimiter !")?;

let format_result =
|color: Color| formats::format_color(color.into(), CssColorFormat::Hex.into(), true);
let format_result = |color: Color| {
let color = if let Color::Oklcha(color) = color {
gamut::gamut_clip_preserve_chroma(color.into()).into()
} else {
color.into()
};
formats::format_normalized_hex_no_alpha(color)
};

let response = if fmt == "auto" {
match formats::parse_color_unknown_format(color) {
Expand Down

0 comments on commit f954b8b

Please sign in to comment.