mirror of
https://github.com/storytold/point-generator.git
synced 2026-10-13 23:52:45 +00:00
added color struct
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum Color {
|
||||
Black,
|
||||
White,
|
||||
Red,
|
||||
Green,
|
||||
Blue,
|
||||
Custom { r: f32, g: f32, b: f32 },
|
||||
}
|
||||
|
||||
impl Color {
|
||||
pub fn get_f32_r(&self) -> f32 {
|
||||
match &self {
|
||||
Color::Black => 0.0f32,
|
||||
Color::White => 1.0f32,
|
||||
Color::Red => 1.0f32,
|
||||
Color::Green => 0.0f32,
|
||||
Color::Blue => 0.0f32,
|
||||
Color::Custom {r, g, b} => *r,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_f32_g(&self) -> f32 {
|
||||
match &self {
|
||||
Color::Black => 0.0f32,
|
||||
Color::White => 1.0f32,
|
||||
Color::Red => 0.0f32,
|
||||
Color::Green => 1.0f32,
|
||||
Color::Blue => 0.0f32,
|
||||
Color::Custom {r, g, b} => *g,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_f32_b(&self) -> f32 {
|
||||
match &self {
|
||||
Color::Black => 0.0f32,
|
||||
Color::White => 1.0f32,
|
||||
Color::Red => 1.0f32,
|
||||
Color::Green => 0.0f32,
|
||||
Color::Blue => 0.0f32,
|
||||
Color::Custom {r, g, b} => *b,
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
-1
@@ -1,9 +1,13 @@
|
||||
//! Generate random points to send over ZeroMQ to a consumer
|
||||
|
||||
mod point;
|
||||
mod color;
|
||||
|
||||
use point::Point;
|
||||
use std::thread;
|
||||
use zmq::Error;
|
||||
use std::time::Duration;
|
||||
use crate::color::Color;
|
||||
|
||||
fn main() {
|
||||
let ctx = zmq::Context::new();
|
||||
@@ -14,7 +18,10 @@ fn main() {
|
||||
//socket.send("hello world!", 0).unwrap();
|
||||
|
||||
loop {
|
||||
let point = Point::white_at(0.0f32, 0.0f32, 0.0f32);
|
||||
let point = Point::at_random_range(-1000.0f32, 1000.0f32, Color::White);
|
||||
|
||||
println!("Location: {}", point.location_string());
|
||||
|
||||
let bytes = point.to_bytes();
|
||||
|
||||
//let result = socket.send("hello world!", 0);
|
||||
|
||||
+33
-4
@@ -1,4 +1,8 @@
|
||||
use bytes::{BytesMut, BufMut};
|
||||
use rand::distributions::Uniform;
|
||||
use rand::Rng;
|
||||
use crate::color::Color;
|
||||
|
||||
|
||||
pub struct Point {
|
||||
pub x: f32,
|
||||
@@ -13,19 +17,40 @@ pub struct Point {
|
||||
|
||||
impl Point {
|
||||
|
||||
pub fn white_at(x: f32, y: f32, z: f32) -> Self {
|
||||
pub fn at(x: f32, y: f32, z: f32, color: Color) -> Self {
|
||||
Point {
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
_unused: 0.0f32,
|
||||
r: 1.0f32,
|
||||
g: 1.0f32,
|
||||
b: 1.0f32,
|
||||
r: color.get_f32_r(),
|
||||
g: color.get_f32_g(),
|
||||
b: color.get_f32_b(),
|
||||
a: 1.0f32,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn at_random_range(min: f32, max: f32, color: Color) -> Self {
|
||||
let mut rng = rand::thread_rng();
|
||||
let range = Uniform::new_inclusive(min, max);
|
||||
|
||||
let x = rng.sample(&range);
|
||||
let y = rng.sample(&range);
|
||||
let z = rng.sample(&range);
|
||||
|
||||
Point {
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
_unused: 0.0f32,
|
||||
r: color.get_f32_r(),
|
||||
g: color.get_f32_g(),
|
||||
b: color.get_f32_b(),
|
||||
a: 1.0f32,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
let mut buf = BytesMut::with_capacity(32);
|
||||
buf.put_f32(self.x);
|
||||
@@ -38,4 +63,8 @@ impl Point {
|
||||
buf.put_f32(self.a);
|
||||
buf.to_vec()
|
||||
}
|
||||
|
||||
pub fn location_string(&self) -> String {
|
||||
format!("{}, {}, {}", self.x, self.y, self.z)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user