summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-02-26 14:44:11 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-02-26 14:44:11 +0700
commit7b0375eee4a945cc8fbb3c03cfc0657e0f1ee443 (patch)
tree6dd01fb883b806ef540a40b1c8ae26a0b81f967f
parent20be72c501397a45e3c8f4bc95a3451f103e3a28 (diff)
upd: mapping 0-1 to byte
-rw-r--r--src/global.rs10
-rw-r--r--src/main.rs6
2 files changed, 11 insertions, 5 deletions
diff --git a/src/global.rs b/src/global.rs
index cb616b1..3a1e329 100644
--- a/src/global.rs
+++ b/src/global.rs
@@ -8,6 +8,16 @@ pub struct Pixel {
pub b: u8,
}
+impl Pixel {
+ pub fn from_frac(r: f32, g: f32, b: f32) -> Self {
+ Self {
+ r: (255.999 * r) as u8,
+ g: (255.999 * g) as u8,
+ b: (255.999 * b) as u8,
+ }
+ }
+}
+
impl Default for Pixel {
fn default() -> Self {
Pixel { r:0, g:0, b:0 }
diff --git a/src/main.rs b/src/main.rs
index 2c81847..abad7ff 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -24,11 +24,7 @@ fn test_render(data: &mut DisplayBuffer) {
let g = j as f32 / IMG_HEIGHT as f32;
let b = 0.0f32;
- let ir = (255.999 * r) as u8;
- let ig = (255.999 * g) as u8;
- let ib = (255.999 * b) as u8;
-
- data[i][j] = Pixel { r: ir, g: ig, b: ib};
+ data[i][j] = Pixel::from_frac(r, g, b);
})
});
}