Reputation: 3421
I'm using the noise
crate and having trouble understanding how to convert their Color
type to an RGB value.
noise = "0.7.0"
pub type Color = [u8; 4];
I'm trying to use the get_value()
function, seen here in the docs as:
pub fn get_value(&self, x: usize, y: usize) -> Color {
let (width, height) = self.size;
if x < width && y < height {
self.map[x + y * width]
} else {
self.border_color
}
}
get_value()
is implemented for PlaneMapBuilder. So I would expect PlaneMapBuilder::get_value(x,y)
to return something of the format [r,g,b,a]
, but this does not happen:
extern crate noise;
use noise::{utils::*, Billow};
fn main() {
let mut my_noise = PlaneMapBuilder::new(&Billow::new()).build();
let my_val = my_noise.get_value(1,1);
println!("{}", my_val.to_string());
///returns something like -0.610765515150546, not a [u8;4] as I would expect
}
In the docs I see this definition of add_gradient_point()
which takes a Color
as a parameter:
pub fn add_gradient_point(mut self, pos: f64, color: Color) -> Self {
// check to see if the vector already contains the input point.
if !self
.gradient_points
.iter()
.any(|&x| (x.pos - pos).abs() < std::f64::EPSILON)
{
// it doesn't, so find the correct position to insert the new
// control point.
let insertion_point = self.find_insertion_point(pos);
// add the new control point at the correct position.
self.gradient_points
.insert(insertion_point, GradientPoint { pos, color });
}
self
}
Here they use the [u8; 4]
structure I would expect for the Color
type:
let jade_gradient = ColorGradient::new()
.clear_gradient()
.add_gradient_point(-1.000, [24, 146, 102, 255])
.add_gradient_point(0.000, [78, 154, 115, 255])
What could explain this behavior?
Upvotes: 2
Views: 366
Reputation: 26197
get_value()
is implemented forPlaneMapBuilder
You are correct that PlaneMapBuilder
"implements" get_value()
. However, it is not get_value()
from NoiseImage
. It is actually NoiseMap
, where its get_value()
returns a f64
and not Color
.
Depending on what kind of "colors" you'd want, then you could instead use ImageRenderer
and call its render()
method with &my_noise
, which returns a NoiseImage
.
// noise = "0.7.0"
use noise::{utils::*, Billow};
fn main() {
let my_noise = PlaneMapBuilder::new(&Billow::new()).build();
let image = ImageRenderer::new().render(&my_noise);
let my_val = image.get_value(1, 1);
println!("{:?}", my_val);
// Prints: `[18, 18, 18, 255]`
}
Here they use the
[u8; 4]
structure I would expect for theColor
type
Just to be clear, those are the same thing in this case. In short the type
keyword allows you to define new "type aliases" for an existing types. Essentially, you'd be able to give a complex type a shorthand name. However, they are still the same type.
Upvotes: 3