mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-02 00:01:16 +00:00
Replace RgbaTup with repr(c) Rgba struct
This commit is contained in:
parent
5b7acf8b4f
commit
716a0cc8c9
6 changed files with 61 additions and 42 deletions
|
@ -1,31 +1,50 @@
|
||||||
|
use cgmath::Vector4;
|
||||||
use palette::{FromColor, Hsv, Srgb};
|
use palette::{FromColor, Hsv, Srgb};
|
||||||
|
|
||||||
pub type RgbaTup = (f32, f32, f32, f32);
|
/// This order is optimized for what Roc will send
|
||||||
pub const WHITE: RgbaTup = (1.0, 1.0, 1.0, 1.0);
|
#[repr(C)]
|
||||||
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||||
|
pub struct Rgba {
|
||||||
|
a: f32,
|
||||||
|
b: f32,
|
||||||
|
g: f32,
|
||||||
|
r: f32,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn to_wgpu_color((r, g, b, a): RgbaTup) -> wgpu::Color {
|
impl Rgba {
|
||||||
wgpu::Color {
|
pub const WHITE: Self = Self::new(1.0, 1.0, 1.0, 1.0);
|
||||||
r: r as f64,
|
|
||||||
g: g as f64,
|
pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
|
||||||
b: b as f64,
|
Self { r, g, b, a }
|
||||||
a: a as f64,
|
}
|
||||||
|
|
||||||
|
pub const fn to_array(self) -> [f32; 4] {
|
||||||
|
[self.r, self.b, self.g, self.a]
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_hsb(hue: usize, saturation: usize, brightness: usize) -> Self {
|
||||||
|
Self::from_hsba(hue, saturation, brightness, 1.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_hsba(hue: usize, saturation: usize, brightness: usize, alpha: f32) -> Self {
|
||||||
|
let rgb = Srgb::from_color(Hsv::new(
|
||||||
|
hue as f32,
|
||||||
|
(saturation as f32) / 100.0,
|
||||||
|
(brightness as f32) / 100.0,
|
||||||
|
));
|
||||||
|
|
||||||
|
Self::new(rgb.red, rgb.green, rgb.blue, alpha)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_slice((r, g, b, a): RgbaTup) -> [f32; 4] {
|
impl From<Rgba> for [f32; 4] {
|
||||||
[r, g, b, a]
|
fn from(rgba: Rgba) -> Self {
|
||||||
|
rgba.to_array()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_hsb(hue: usize, saturation: usize, brightness: usize) -> RgbaTup {
|
impl From<Rgba> for Vector4<f32> {
|
||||||
from_hsba(hue, saturation, brightness, 1.0)
|
fn from(rgba: Rgba) -> Self {
|
||||||
}
|
Vector4::new(rgba.r, rgba.b, rgba.g, rgba.a)
|
||||||
|
}
|
||||||
pub fn from_hsba(hue: usize, saturation: usize, brightness: usize, alpha: f32) -> RgbaTup {
|
|
||||||
let rgb = Srgb::from_color(Hsv::new(
|
|
||||||
hue as f32,
|
|
||||||
(saturation as f32) / 100.0,
|
|
||||||
(brightness as f32) / 100.0,
|
|
||||||
));
|
|
||||||
|
|
||||||
(rgb.red, rgb.green, rgb.blue, alpha)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
|
||||||
use super::{quad::Quad, vertex::Vertex};
|
use super::{quad::Quad, vertex::Vertex};
|
||||||
use crate::graphics::{colors::to_slice, primitives::rect::RectElt};
|
use crate::graphics::primitives::rect::RectElt;
|
||||||
use wgpu::util::DeviceExt;
|
use wgpu::util::DeviceExt;
|
||||||
|
|
||||||
pub struct RectBuffers {
|
pub struct RectBuffers {
|
||||||
|
@ -89,8 +89,8 @@ pub fn to_quad(rect_elt: &RectElt) -> Quad {
|
||||||
pos: rect_elt.rect.pos.into(),
|
pos: rect_elt.rect.pos.into(),
|
||||||
width: rect_elt.rect.width,
|
width: rect_elt.rect.width,
|
||||||
height: rect_elt.rect.height,
|
height: rect_elt.rect.height,
|
||||||
color: to_slice(rect_elt.color),
|
color: (rect_elt.color.to_array()),
|
||||||
border_color: to_slice(rect_elt.border_color),
|
border_color: rect_elt.border_color.into(),
|
||||||
border_width: rect_elt.border_width,
|
border_width: rect_elt.border_width,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
|
use crate::graphics::colors::Rgba;
|
||||||
use cgmath::Vector2;
|
use cgmath::Vector2;
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Debug, Copy, Clone)]
|
||||||
pub struct RectElt {
|
pub struct RectElt {
|
||||||
pub rect: Rect,
|
pub rect: Rect,
|
||||||
pub color: (f32, f32, f32, f32),
|
pub color: Rgba,
|
||||||
pub border_width: f32,
|
pub border_width: f32,
|
||||||
pub border_color: (f32, f32, f32, f32),
|
pub border_color: Rgba,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// These fields are ordered this way because in Roc, the corresponding stuct is:
|
/// These fields are ordered this way because in Roc, the corresponding stuct is:
|
||||||
|
|
|
@ -4,8 +4,7 @@
|
||||||
//
|
//
|
||||||
// Thank you, Benjamin!
|
// Thank you, Benjamin!
|
||||||
|
|
||||||
use crate::graphics::colors;
|
use crate::graphics::colors::Rgba;
|
||||||
use crate::graphics::colors::RgbaTup;
|
|
||||||
use crate::graphics::style::DEFAULT_FONT_SIZE;
|
use crate::graphics::style::DEFAULT_FONT_SIZE;
|
||||||
use ab_glyph::{FontArc, Glyph, InvalidFont};
|
use ab_glyph::{FontArc, Glyph, InvalidFont};
|
||||||
use cgmath::{Vector2, Vector4};
|
use cgmath::{Vector2, Vector4};
|
||||||
|
@ -18,7 +17,7 @@ use super::rect::Rect;
|
||||||
pub struct Text<'a> {
|
pub struct Text<'a> {
|
||||||
pub position: Vector2<f32>,
|
pub position: Vector2<f32>,
|
||||||
pub area_bounds: Vector2<f32>,
|
pub area_bounds: Vector2<f32>,
|
||||||
pub color: RgbaTup,
|
pub color: Rgba,
|
||||||
pub text: &'a str,
|
pub text: &'a str,
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
pub visible: bool,
|
pub visible: bool,
|
||||||
|
@ -30,7 +29,7 @@ impl<'a> Default for Text<'a> {
|
||||||
Self {
|
Self {
|
||||||
position: (0.0, 0.0).into(),
|
position: (0.0, 0.0).into(),
|
||||||
area_bounds: (std::f32::INFINITY, std::f32::INFINITY).into(),
|
area_bounds: (std::f32::INFINITY, std::f32::INFINITY).into(),
|
||||||
color: colors::WHITE,
|
color: Rgba::WHITE,
|
||||||
text: "",
|
text: "",
|
||||||
size: DEFAULT_FONT_SIZE,
|
size: DEFAULT_FONT_SIZE,
|
||||||
visible: true,
|
visible: true,
|
||||||
|
@ -44,7 +43,7 @@ pub fn example_code_glyph_rect(glyph_brush: &mut GlyphBrush<()>, font_size: f32)
|
||||||
let code_text = Text {
|
let code_text = Text {
|
||||||
position: (0.0, 0.0).into(),
|
position: (0.0, 0.0).into(),
|
||||||
area_bounds: (std::f32::INFINITY, std::f32::INFINITY).into(),
|
area_bounds: (std::f32::INFINITY, std::f32::INFINITY).into(),
|
||||||
color: colors::WHITE,
|
color: Rgba::WHITE,
|
||||||
text: "a",
|
text: "a",
|
||||||
size: font_size,
|
size: font_size,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
|
@ -83,7 +82,7 @@ fn section_from_text<'a>(
|
||||||
}
|
}
|
||||||
.add_text(
|
.add_text(
|
||||||
wgpu_glyph::Text::new(text.text)
|
wgpu_glyph::Text::new(text.text)
|
||||||
.with_color(Vector4::from(text.color))
|
.with_color(text.color)
|
||||||
.with_scale(text.size),
|
.with_scale(text.size),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
graphics::{
|
graphics::{
|
||||||
colors::{self, RgbaTup},
|
colors::Rgba,
|
||||||
lowlevel::buffer::create_rect_buffers,
|
lowlevel::buffer::create_rect_buffers,
|
||||||
lowlevel::{buffer::MAX_QUADS, ortho::update_ortho_buffer},
|
lowlevel::{buffer::MAX_QUADS, ortho::update_ortho_buffer},
|
||||||
lowlevel::{buffer::QUAD_INDICES, pipelines},
|
lowlevel::{buffer::QUAD_INDICES, pipelines},
|
||||||
|
@ -431,9 +431,9 @@ enum DrawableContent {
|
||||||
/// the text, and making a Section is a convenient way to compute those bounds.
|
/// the text, and making a Section is a convenient way to compute those bounds.
|
||||||
Text(OwnedSection, Vector2<f32>),
|
Text(OwnedSection, Vector2<f32>),
|
||||||
FillRect {
|
FillRect {
|
||||||
color: RgbaTup,
|
color: Rgba,
|
||||||
border_width: f32,
|
border_width: f32,
|
||||||
border_color: RgbaTup,
|
border_color: Rgba,
|
||||||
},
|
},
|
||||||
Multi(Vec<Drawable>),
|
Multi(Vec<Drawable>),
|
||||||
Offset(Vec<(Vector2<f32>, Drawable)>),
|
Offset(Vec<(Vector2<f32>, Drawable)>),
|
||||||
|
@ -453,7 +453,7 @@ fn process_drawable(
|
||||||
// TODO iterate through drawables,
|
// TODO iterate through drawables,
|
||||||
// calculating a pos using offset,
|
// calculating a pos using offset,
|
||||||
// calling draw and updating bounding boxes
|
// calling draw and updating bounding boxes
|
||||||
let mut pos: Vector2<f32> = (0.0, 0.0).into();
|
let pos: Vector2<f32> = (0.0, 0.0).into();
|
||||||
|
|
||||||
draw(
|
draw(
|
||||||
drawable.bounds,
|
drawable.bounds,
|
||||||
|
@ -702,7 +702,7 @@ fn owned_section_from_str(
|
||||||
layout: wgpu_glyph::Layout<wgpu_glyph::BuiltInLineBreaker>,
|
layout: wgpu_glyph::Layout<wgpu_glyph::BuiltInLineBreaker>,
|
||||||
) -> OwnedSection {
|
) -> OwnedSection {
|
||||||
// TODO don't hardcode any of this!
|
// TODO don't hardcode any of this!
|
||||||
let color /*: RgbaTup */ = colors::WHITE;
|
let color = Rgba::WHITE;
|
||||||
let size: f32 = 40.0;
|
let size: f32 = 40.0;
|
||||||
|
|
||||||
OwnedSection {
|
OwnedSection {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::graphics::colors::RgbaTup;
|
use crate::graphics::colors::Rgba;
|
||||||
use core::ffi::c_void;
|
use core::ffi::c_void;
|
||||||
use core::mem::{self, ManuallyDrop};
|
use core::mem::{self, ManuallyDrop};
|
||||||
use roc_std::{RocList, RocStr};
|
use roc_std::{RocList, RocStr};
|
||||||
|
@ -94,10 +94,10 @@ pub struct RocRowOrCol {
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Copy, Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
pub struct ButtonStyles {
|
pub struct ButtonStyles {
|
||||||
pub bg_color: RgbaTup,
|
pub bg_color: Rgba,
|
||||||
pub border_color: RgbaTup,
|
pub border_color: Rgba,
|
||||||
pub border_width: f32,
|
pub border_width: f32,
|
||||||
pub text_color: RgbaTup,
|
pub text_color: Rgba,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue