Change color from RGBAF32 to RGBA8
This commit is contained in:
parent
dea255cf1d
commit
25e60c683b
|
@ -8,16 +8,17 @@ use ouroboros::self_referencing;
|
|||
#[derive(Copy, Clone)]
|
||||
pub struct Vertex {
|
||||
pub position: [f32; 2],
|
||||
pub color: [f32; 4],
|
||||
pub color: [u8; 4],
|
||||
}
|
||||
|
||||
glium::implement_vertex!(Vertex, position, color);
|
||||
glium::implement_vertex!(Vertex, position normalize(false), color normalize(true));
|
||||
|
||||
impl From<&canary::MeshVertex> for Vertex {
|
||||
fn from(v: &canary::MeshVertex) -> Self {
|
||||
let (r, g, b, a) = v.color.to_rgba_unmultiplied();
|
||||
Self {
|
||||
position: [v.position.x, v.position.y],
|
||||
color: [v.color.r, v.color.g, v.color.b, v.color.a],
|
||||
color: [r, g, b, a],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -157,7 +157,7 @@ pub struct DrawContext {
|
|||
panel: Panel,
|
||||
offset: Option<Vec2>,
|
||||
clip_rect: Option<Rect>,
|
||||
opacity: Option<f32>,
|
||||
opacity: Option<u8>,
|
||||
}
|
||||
|
||||
impl DrawContext {
|
||||
|
@ -205,7 +205,7 @@ impl DrawContext {
|
|||
}
|
||||
|
||||
if let Some(opacity) = self.opacity.as_ref() {
|
||||
color.a *= opacity;
|
||||
color = color.alpha_multiply(*opacity);
|
||||
}
|
||||
|
||||
self.panel
|
||||
|
@ -370,7 +370,7 @@ impl DrawContext {
|
|||
}
|
||||
|
||||
if let Some(opacity) = self.opacity {
|
||||
color.a *= opacity;
|
||||
color = color.alpha_multiply(opacity);
|
||||
}
|
||||
|
||||
self.panel
|
||||
|
@ -404,9 +404,9 @@ impl DrawContext {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn with_opacity(&self, mut opacity: f32) -> Self {
|
||||
pub fn with_opacity(&self, mut opacity: u8) -> Self {
|
||||
if let Some(old) = self.opacity {
|
||||
opacity *= old;
|
||||
opacity = (((opacity as u16) * (old as u16)) >> 8) as u8;
|
||||
}
|
||||
|
||||
Self {
|
||||
|
|
|
@ -108,11 +108,7 @@ impl Panel {
|
|||
}
|
||||
|
||||
pub fn draw_text_layout(&self, layout: &TextLayout, offset: Vec2, scale: f32, color: Color) {
|
||||
unsafe {
|
||||
draw_text_layout(
|
||||
layout.0, offset.x, offset.y, scale, color.r, color.g, color.b, color.a,
|
||||
)
|
||||
}
|
||||
unsafe { draw_text_layout(layout.0, offset.x, offset.y, scale, color.0) }
|
||||
}
|
||||
|
||||
pub fn draw_triangle(&self, v1: Vec2, v2: Vec2, v3: Vec2, color: Color) {
|
||||
|
@ -194,7 +190,7 @@ impl Message {
|
|||
|
||||
extern "C" {
|
||||
fn draw_indexed(vertices_ptr: u32, vertices_num: u32, indices_ptr: u32, indices_num: u32);
|
||||
fn draw_text_layout(id: u32, xoff: f32, yoff: f32, scale: f32, r: f32, g: f32, b: f32, a: f32);
|
||||
fn draw_text_layout(id: u32, xoff: f32, yoff: f32, scale: f32, color: u32);
|
||||
fn font_load(family_ptr: u32, family_len: u32) -> u32;
|
||||
|
||||
fn text_layout_new(font_id: u32, text_ptr: u32, text_len: u32) -> u32;
|
||||
|
|
|
@ -28,31 +28,40 @@ impl Vec2 {
|
|||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug, Pod, Zeroable)]
|
||||
pub struct Color {
|
||||
pub r: f32,
|
||||
pub g: f32,
|
||||
pub b: f32,
|
||||
pub a: f32,
|
||||
}
|
||||
pub struct Color(pub u32);
|
||||
|
||||
impl Color {
|
||||
pub const WHITE: Self = Self::new(1., 1., 1., 1.);
|
||||
pub const BLACK: Self = Self::new(0., 0., 0., 1.);
|
||||
pub const TRANSPARENT: Self = Self::new(0., 0., 0., 0.);
|
||||
pub const RED: Self = Self::new(1., 0., 0., 1.);
|
||||
pub const GREEN: Self = Self::new(0., 1., 0., 1.);
|
||||
pub const BLUE: Self = Self::new(0., 0., 1., 1.);
|
||||
pub const YELLOW: Self = Self::new(1., 1., 0., 1.);
|
||||
pub const MAGENTA: Self = Self::new(1., 0., 1., 1.);
|
||||
pub const CYAN: Self = Self::new(0., 1., 1., 1.);
|
||||
pub const WHITE: Self = Self(0xffffffff);
|
||||
pub const BLACK: Self = Self(0x000000ff);
|
||||
pub const TRANSPARENT: Self = Self(0);
|
||||
pub const RED: Self = Self(0xff0000ff);
|
||||
pub const GREEN: Self = Self(0x00ff00ff);
|
||||
pub const BLUE: Self = Self(0x0000ffff);
|
||||
pub const YELLOW: Self = Self(0xffff00ff);
|
||||
pub const MAGENTA: Self = Self(0xff00ffff);
|
||||
pub const CYAN: Self = Self(0x00ffffff);
|
||||
|
||||
pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
|
||||
Self { r, g, b, a }
|
||||
pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
|
||||
Color(((r as u32) << 24) | ((g as u32) << 16) | ((b as u32) << 8) | (a as u32))
|
||||
}
|
||||
|
||||
pub fn to_rgba_unmultiplied(&self) -> (u8, u8, u8, u8) {
|
||||
let map = |c: f32| (c * 255.0).floor() as u8;
|
||||
(map(self.r), map(self.g), map(self.b), map(self.a))
|
||||
(
|
||||
(self.0 >> 24) as u8,
|
||||
(self.0 >> 16) as u8,
|
||||
(self.0 >> 8) as u8,
|
||||
self.0 as u8,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn alpha_multiply(&self, mul: u8) -> Self {
|
||||
let a = self.0 as u8 as u16;
|
||||
let multiplied = ((a * (mul as u16)) >> 8) as u8;
|
||||
self.with_alpha(multiplied)
|
||||
}
|
||||
|
||||
pub fn with_alpha(&self, alpha: u8) -> Self {
|
||||
Self(self.0 & 0xffffff00 | alpha as u32)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -108,14 +117,16 @@ mod glam_interop {
|
|||
|
||||
impl From<glam::Vec4> for Color {
|
||||
fn from(other: glam::Vec4) -> Self {
|
||||
Self::new(other.x, other.y, other.z, other.w)
|
||||
let map = |v: f32| (v * 255.0).floor() as u8;
|
||||
Self::new(map(other.x), map(other.y), map(other.z), map(other.w))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Color> for glam::Vec4 {
|
||||
fn from(other: Color) -> Self {
|
||||
let Color { r, g, b, a } = other;
|
||||
Self::new(r, g, b, a)
|
||||
let (r, g, b, a) = other.to_rgba_unmultiplied();
|
||||
let map = |v: u8| (v as f32) / 255.0;
|
||||
Self::new(map(r), map(g), map(b), map(a))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use keyframe::EasingFunction;
|
||||
use crate::Color;
|
||||
use keyframe::EasingFunction;
|
||||
|
||||
pub trait AnimationLerp<T> {
|
||||
fn lerp(&self, x: f32) -> T;
|
||||
|
@ -105,6 +105,15 @@ impl<F: EasingFunction> AnimationLerp<Color> for Animation<F, Color> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<F: EasingFunction> AnimationLerp<u8> for Animation<F, u8> {
|
||||
fn lerp(&self, x: f32) -> u8 {
|
||||
let from = self.from as f32;
|
||||
let to = self.to as f32;
|
||||
let lerp = (1.0 - x) * from + x * to;
|
||||
lerp.round() as _
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, T> Animation<F, T>
|
||||
where
|
||||
F: EasingFunction,
|
||||
|
|
|
@ -123,9 +123,9 @@ impl Default for RectButtonStyle {
|
|||
label_baseline: 0.25,
|
||||
icon_scale_factor: 0.8,
|
||||
icon_margin_factor: 1.1,
|
||||
inactive_color: Color::new(1., 1., 1., 0.2),
|
||||
hover_color: Color::new(1., 1., 1., 0.8),
|
||||
selected_color: Color::new(1., 1., 0., 1.),
|
||||
inactive_color: Color::WHITE.with_alpha(0x40),
|
||||
hover_color: Color::WHITE.with_alpha(0xb0),
|
||||
selected_color: Color::YELLOW,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ pub struct DialogBodyStyle {
|
|||
impl Default for DialogBodyStyle {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
color: Color::new(1., 1., 1., 0.8),
|
||||
color: Color::WHITE.with_alpha(0xb0),
|
||||
height: 0.6,
|
||||
text_font: Font::new(crate::CONTENT_FONT),
|
||||
text_color: Color::BLACK,
|
||||
|
|
|
@ -26,7 +26,7 @@ pub enum SlotMenuEvent {
|
|||
pub struct SlotMenuButton<T> {
|
||||
pub widget: T,
|
||||
pub slide_anim: Animation<EaseOut>,
|
||||
pub opacity_anim: Animation<EaseOut>,
|
||||
pub opacity_anim: Animation<EaseOut, u8>,
|
||||
}
|
||||
|
||||
pub struct SlotMenu<T> {
|
||||
|
@ -57,7 +57,7 @@ impl<T> SlotMenu<T> {
|
|||
slide_anim.set_out_delay(out_delay);
|
||||
slide_anim.ease_in();
|
||||
|
||||
let mut opacity_anim = Animation::new(EaseOut, duration, 0.0, 1.0);
|
||||
let mut opacity_anim = Animation::new(EaseOut, duration, 0, 0xff);
|
||||
opacity_anim.set_in_delay(in_delay);
|
||||
opacity_anim.set_out_delay(out_delay);
|
||||
opacity_anim.ease_in();
|
||||
|
@ -178,7 +178,7 @@ impl<T: Widget + Button> Widget for SlotMenu<T> {
|
|||
let ctx = ctx.with_offset(Vec2::new(0.0, y));
|
||||
|
||||
let opacity = button.opacity_anim.get();
|
||||
let ctx = if opacity != 1.0 {
|
||||
let ctx = if opacity != u8::MAX {
|
||||
ctx.with_opacity(opacity)
|
||||
} else {
|
||||
ctx
|
||||
|
@ -348,12 +348,7 @@ impl Container for TabMenu {
|
|||
}
|
||||
|
||||
fn draw(&mut self, ctx: &DrawContext) {
|
||||
let head_color = Color {
|
||||
r: 1.0,
|
||||
g: 1.0,
|
||||
b: 1.0,
|
||||
a: 1.0,
|
||||
};
|
||||
let head_color = Color::WHITE;
|
||||
|
||||
ctx.draw_partially_rounded_rect(
|
||||
CornerFlags::BOTTOM_RIGHT,
|
||||
|
|
|
@ -21,11 +21,11 @@ impl Default for ScrollBarStyle {
|
|||
margin: Vec2::new(0.01, 0.01),
|
||||
body_radius: 0.005,
|
||||
body_width: 0.015,
|
||||
body_idle_color: Color::new(0.5, 0.5, 0.5, 1.0),
|
||||
body_hover_color: Color::new(0.8, 0.8, 0.8, 1.0),
|
||||
body_selected_color: Color::new(1.0, 1.0, 0.0, 1.0),
|
||||
body_idle_color: Color(0x7f7f7fff),
|
||||
body_hover_color: Color(0xb0b0b0ff),
|
||||
body_selected_color: Color::MAGENTA,
|
||||
rail_width: 0.005,
|
||||
rail_color: Color::new(0.7, 0.7, 0.7, 0.5),
|
||||
rail_color: Color(0xa0a0a07f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ macro_rules! impl_shell_inner {
|
|||
pub struct Reveal<T> {
|
||||
inner: T,
|
||||
slide_anim: Animation<EaseIn>,
|
||||
opacity_anim: Animation<Linear>,
|
||||
opacity_anim: Animation<Linear, u8>,
|
||||
state: bool,
|
||||
}
|
||||
|
||||
|
@ -36,7 +36,7 @@ impl<T: Widget> Reveal<T> {
|
|||
Self {
|
||||
inner,
|
||||
slide_anim: Animation::new(EaseIn, duration, slide, 0.0),
|
||||
opacity_anim: Animation::new(Linear, duration, 0.0, 1.0),
|
||||
opacity_anim: Animation::new(Linear, duration, 0, 0xff),
|
||||
state: false,
|
||||
}
|
||||
}
|
||||
|
|
13
src/lib.rs
13
src/lib.rs
|
@ -131,12 +131,9 @@ impl<T: ScriptAbi> WasmtimeScript<T> {
|
|||
xoff: f32,
|
||||
yoff: f32,
|
||||
scale: f32,
|
||||
r: f32,
|
||||
g: f32,
|
||||
b: f32,
|
||||
a: f32| {
|
||||
color: u32| {
|
||||
let offset = Vec2 { x: xoff, y: yoff };
|
||||
let color = Color { r, g, b, a };
|
||||
let color = Color(color);
|
||||
caller.data().draw_text_layout(id, offset, scale, color);
|
||||
},
|
||||
)?;
|
||||
|
@ -177,9 +174,7 @@ impl<T: ScriptAbi> WasmtimeScript<T> {
|
|||
linker.func_wrap(
|
||||
module,
|
||||
"message_get_len",
|
||||
|caller: wasmtime::Caller<'_, T>, id: u32| -> u32 {
|
||||
caller.data().message_get_len(id)
|
||||
}
|
||||
|caller: wasmtime::Caller<'_, T>, id: u32| -> u32 { caller.data().message_get_len(id) },
|
||||
)?;
|
||||
|
||||
linker.func_wrap(
|
||||
|
@ -190,7 +185,7 @@ impl<T: ScriptAbi> WasmtimeScript<T> {
|
|||
let len = caller.data().message_get_len(id) as usize;
|
||||
let dst = Self::get_memory_slice_bytes(&mut caller, ptr, len);
|
||||
caller.data().message_get_data(id, dst)
|
||||
}
|
||||
},
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
|
|
Loading…
Reference in New Issue