-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6f00567
commit 505cfe4
Showing
2 changed files
with
45 additions
and
43 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,47 +1,52 @@ | ||
use bevy_color::Color; | ||
use clap::ValueEnum; | ||
use cli::CliColorFormat; | ||
use mlua::prelude::*; | ||
mod cli; | ||
mod formats; | ||
mod gamut; | ||
|
||
pub mod cli; | ||
pub mod formats; | ||
pub mod gamut; | ||
#[cfg(not(target_arch = "wasm32"))] | ||
mod lua { | ||
use super::*; | ||
use bevy_color::Color; | ||
use clap::ValueEnum; | ||
use cli::CliColorFormat; | ||
use mlua::prelude::*; | ||
|
||
fn gamut_clip(color: Color) -> Color { | ||
if let Color::Oklcha(color) = color { | ||
gamut::gamut_clip_preserve_chroma(color.into()).into() | ||
} else { | ||
color | ||
fn gamut_clip(color: Color) -> Color { | ||
if let Color::Oklcha(color) = color { | ||
gamut::gamut_clip_preserve_chroma(color.into()).into() | ||
} else { | ||
color | ||
} | ||
} | ||
} | ||
|
||
fn version(_: &Lua, _: ()) -> LuaResult<&'static str> { | ||
Ok(env!("CARGO_PKG_VERSION")) | ||
} | ||
fn version(_: &Lua, _: ()) -> LuaResult<&'static str> { | ||
Ok(env!("CARGO_PKG_VERSION")) | ||
} | ||
|
||
fn color_to_hex(_: &Lua, (color, fmt): (String, Option<String>)) -> LuaResult<Option<String>> { | ||
let color = if let Some(fmt) = fmt { | ||
let parsed_fmt = CliColorFormat::from_str(&fmt, true).map_err(LuaError::RuntimeError)?; | ||
match formats::parse_color(&color, parsed_fmt.into()) { | ||
Some((c, _)) => c, | ||
None => return Ok(None), | ||
} | ||
} else { | ||
match formats::parse_color_unknown_format(&color) { | ||
Some((c, _, _)) => c, | ||
None => return Ok(None), | ||
} | ||
}; | ||
fn color_to_hex(_: &Lua, (color, fmt): (String, Option<String>)) -> LuaResult<Option<String>> { | ||
let color = if let Some(fmt) = fmt { | ||
let parsed_fmt = | ||
CliColorFormat::from_str(&fmt, true).map_err(LuaError::RuntimeError)?; | ||
match formats::parse_color(&color, parsed_fmt.into()) { | ||
Some((c, _)) => c, | ||
None => return Ok(None), | ||
} | ||
} else { | ||
match formats::parse_color_unknown_format(&color) { | ||
Some((c, _, _)) => c, | ||
None => return Ok(None), | ||
} | ||
}; | ||
|
||
let color = gamut_clip(color); | ||
let color = gamut_clip(color); | ||
|
||
Ok(Some(formats::format_normalized_hex_no_alpha(color.into()))) | ||
} | ||
Ok(Some(formats::format_normalized_hex_no_alpha(color.into()))) | ||
} | ||
|
||
#[mlua::lua_module(skip_memory_check)] | ||
fn parser_lua_module(lua: &Lua) -> LuaResult<LuaTable> { | ||
let exports = lua.create_table()?; | ||
exports.set("color_to_hex", lua.create_function(color_to_hex)?)?; | ||
exports.set("version", lua.create_function(version)?)?; | ||
Ok(exports) | ||
#[mlua::lua_module(skip_memory_check)] | ||
fn parser_lua_module(lua: &Lua) -> LuaResult<LuaTable> { | ||
let exports = lua.create_table()?; | ||
exports.set("color_to_hex", lua.create_function(color_to_hex)?)?; | ||
exports.set("version", lua.create_function(version)?)?; | ||
Ok(exports) | ||
} | ||
} |