update color functions to work with arrays instead of bare components

This commit is contained in:
Gered 2023-04-18 16:42:13 -04:00
parent 88bea61bab
commit 755d13eef9
14 changed files with 176 additions and 178 deletions

View file

@ -22,9 +22,9 @@ pub fn criterion_benchmark(c: &mut Criterion) {
let texcoord_0_1 = Vector2::new(0.0, 1.0);
let texcoord_1_1 = Vector2::new(1.0, 1.0);
let color_1 = to_argb32(255, 255, 0, 0);
let color_2 = to_argb32(255, 0, 255, 0);
let color_3 = to_argb32(255, 0, 0, 255);
let color_1 = to_argb32([255, 255, 0, 0]);
let color_2 = to_argb32([255, 0, 255, 0]);
let color_3 = to_argb32([255, 0, 0, 255]);
c.bench_function("rgbabitmap_triangle_2d_solid_color", |b| {
let triangle = RgbaTriangle2d::Solid { position: [big_v1, big_v2, big_v3], color: 5 };

View file

@ -242,7 +242,7 @@ impl IndexedBitmap {
for i in 0..=255 {
let argb = palette[i];
let (r, g, b) = from_rgb32(argb);
let [r, g, b] = from_rgb32(argb);
writer.write_u8(r)?;
writer.write_u8(g)?;
writer.write_u8(b)?;

View file

@ -340,14 +340,14 @@ impl ScanlinePixelConverter<u32> for ScanlineBuffer {
let r = self.current[offset];
let g = self.current[offset + 1];
let b = self.current[offset + 2];
Ok(to_rgb32(r, g, b))
Ok(to_rgb32([r, g, b]))
}
ColorFormat::RGBA => {
let r = self.current[offset];
let g = self.current[offset + 1];
let b = self.current[offset + 2];
let a = self.current[offset + 3];
Ok(to_argb32(a, r, g, b))
Ok(to_argb32([a, r, g, b]))
}
_ => Err(PngError::BadFile(format!("Unsupported color format for this PixelReader: {:?}", self.format))),
}
@ -357,14 +357,14 @@ impl ScanlinePixelConverter<u32> for ScanlineBuffer {
let offset = x * self.bpp;
match self.format {
ColorFormat::RGB => {
let (r, g, b) = from_rgb32(pixel);
let [r, g, b] = from_rgb32(pixel);
self.current[offset] = r;
self.current[offset + 1] = g;
self.current[offset + 2] = b;
Ok(())
}
ColorFormat::RGBA => {
let (a, r, g, b) = from_argb32(pixel);
let [a, r, g, b] = from_argb32(pixel);
self.current[offset] = r;
self.current[offset + 1] = g;
self.current[offset + 2] = b;

View file

@ -29,7 +29,7 @@ impl RgbaBitmap {
///
/// returns: `Result<Bitmap, BitmapError>`
pub fn new(width: u32, height: u32) -> Result<Self, BitmapError> {
Self::internal_new(width, height, to_rgb32(0, 0, 0))
Self::internal_new(width, height, to_rgb32([0, 0, 0]))
}
pub fn from_bytes<T: ReadBytesExt>(
@ -46,14 +46,14 @@ impl RgbaBitmap {
let g = reader.read_u8()?;
let b = reader.read_u8()?;
let a = reader.read_u8()?;
to_argb32(a, r, g, b)
to_argb32([a, r, g, b])
}
RgbaPixelFormat::ARGB => {
let a = reader.read_u8()?;
let r = reader.read_u8()?;
let g = reader.read_u8()?;
let b = reader.read_u8()?;
to_argb32(a, r, g, b)
to_argb32([a, r, g, b])
}
};
}

View file

@ -92,9 +92,9 @@ impl RgbaBitmap {
pub fn solid_multicolor_triangle_2d(&mut self, positions: &[Vector2; 3], colors: &[u32; 3]) {
let area = edge_function(positions[0], positions[1], positions[2]);
let (r1, g1, b1) = from_rgb32(colors[0]);
let (r2, g2, b2) = from_rgb32(colors[1]);
let (r3, g3, b3) = from_rgb32(colors[2]);
let [r1, g1, b1] = from_rgb32(colors[0]);
let [r2, g2, b2] = from_rgb32(colors[1]);
let [r3, g3, b3] = from_rgb32(colors[2]);
per_pixel_triangle_2d(
self, //
positions[0],
@ -104,7 +104,7 @@ impl RgbaBitmap {
let r = ((w0 * r1 as f32 + w1 * r2 as f32 + w2 * r3 as f32) / area) as u8;
let g = ((w0 * g1 as f32 + w1 * g2 as f32 + w2 * g3 as f32) / area) as u8;
let b = ((w0 * b1 as f32 + w1 * b2 as f32 + w2 * b3 as f32) / area) as u8;
*dest_pixels = to_rgb32(r, g, b)
*dest_pixels = to_rgb32([r, g, b])
},
)
}
@ -116,9 +116,9 @@ impl RgbaBitmap {
blend: BlendFunction,
) {
let area = edge_function(positions[0], positions[1], positions[2]);
let (a1, r1, g1, b1) = from_argb32(colors[0]);
let (a2, r2, g2, b2) = from_argb32(colors[1]);
let (a3, r3, g3, b3) = from_argb32(colors[2]);
let [a1, r1, g1, b1] = from_argb32(colors[0]);
let [a2, r2, g2, b2] = from_argb32(colors[1]);
let [a3, r3, g3, b3] = from_argb32(colors[2]);
per_pixel_triangle_2d(
self, //
positions[0],
@ -129,7 +129,7 @@ impl RgbaBitmap {
let r = ((w0 * r1 as f32 + w1 * r2 as f32 + w2 * r3 as f32) / area) as u8;
let g = ((w0 * g1 as f32 + w1 * g2 as f32 + w2 * g3 as f32) / area) as u8;
let b = ((w0 * b1 as f32 + w1 * b2 as f32 + w2 * b3 as f32) / area) as u8;
*dest_pixels = blend.blend(to_argb32(a, r, g, b), *dest_pixels)
*dest_pixels = blend.blend(to_argb32([a, r, g, b]), *dest_pixels)
},
)
}
@ -201,9 +201,9 @@ impl RgbaBitmap {
bitmap: &Self,
) {
let area = edge_function(positions[0], positions[1], positions[2]);
let (r1, g1, b1) = from_rgb32(colors[0]);
let (r2, g2, b2) = from_rgb32(colors[1]);
let (r3, g3, b3) = from_rgb32(colors[2]);
let [r1, g1, b1] = from_rgb32(colors[0]);
let [r2, g2, b2] = from_rgb32(colors[1]);
let [r3, g3, b3] = from_rgb32(colors[2]);
per_pixel_triangle_2d(
self, //
positions[0],
@ -215,7 +215,7 @@ impl RgbaBitmap {
let b = ((w0 * b1 as f32 + w1 * b2 as f32 + w2 * b3 as f32) / area) as u8;
let u = (w0 * texcoords[0].x + w1 * texcoords[1].x + w2 * texcoords[2].x) / area;
let v = (w0 * texcoords[0].y + w1 * texcoords[1].y + w2 * texcoords[2].y) / area;
*dest_pixels = multiply_argb32(bitmap.sample_at(u, v), to_rgb32(r, g, b))
*dest_pixels = multiply_argb32(bitmap.sample_at(u, v), to_rgb32([r, g, b]))
},
)
}
@ -229,9 +229,9 @@ impl RgbaBitmap {
blend: BlendFunction,
) {
let area = edge_function(positions[0], positions[1], positions[2]);
let (a1, r1, g1, b1) = from_argb32(colors[0]);
let (a2, r2, g2, b2) = from_argb32(colors[1]);
let (a3, r3, g3, b3) = from_argb32(colors[2]);
let [a1, r1, g1, b1] = from_argb32(colors[0]);
let [a2, r2, g2, b2] = from_argb32(colors[1]);
let [a3, r3, g3, b3] = from_argb32(colors[2]);
per_pixel_triangle_2d(
self, //
positions[0],
@ -244,7 +244,7 @@ impl RgbaBitmap {
let b = ((w0 * b1 as f32 + w1 * b2 as f32 + w2 * b3 as f32) / area) as u8;
let u = (w0 * texcoords[0].x + w1 * texcoords[1].x + w2 * texcoords[2].x) / area;
let v = (w0 * texcoords[0].y + w1 * texcoords[1].y + w2 * texcoords[2].y) / area;
let src = multiply_argb32(bitmap.sample_at(u, v), to_argb32(a, r, g, b));
let src = multiply_argb32(bitmap.sample_at(u, v), to_argb32([a, r, g, b]));
*dest_pixels = blend.blend(src, *dest_pixels)
},
)

View file

@ -76,8 +76,8 @@ impl BlendMap {
let mut blend_map = Self::new(source_color, source_color);
for idx in 0..=255 {
let (r, g, b) = from_rgb32(palette[idx]);
let lit = (luminance(r, g, b) * 255.0) as u8;
let rgb = from_rgb32(palette[idx]);
let lit = (luminance(rgb) * 255.0) as u8;
blend_map
.set_mapping(
source_color,
@ -105,11 +105,11 @@ impl BlendMap {
let mut blend_map = BlendMap::new(0, 255);
for source_color in 0..=255 {
let (r, g, b) = from_rgb32(palette[source_color]);
let source_luminance = luminance(r, g, b);
let source_rgb = from_rgb32(palette[source_color]);
let source_luminance = luminance(source_rgb);
for dest_color in 0..=255 {
let (r, g, b) = from_rgb32(palette[dest_color]);
let destination_luminance = luminance(r, g, b);
let dest_rgb = from_rgb32(palette[dest_color]);
let destination_luminance = luminance(dest_rgb);
let weight = (f(source_luminance, destination_luminance) * 255.0) as u8;
blend_map
.set_mapping(
@ -135,10 +135,10 @@ impl BlendMap {
pub fn new_translucency_map(blend_r: f32, blend_g: f32, blend_b: f32, palette: &Palette) -> Self {
let mut blend_map = BlendMap::new(0, 255);
for source in 0..=255 {
let (source_r, source_g, source_b) = from_rgb32(palette[source]);
let [source_r, source_g, source_b] = from_rgb32(palette[source]);
let mapping = blend_map.get_mapping_mut(source).unwrap();
for dest in 0..=255 {
let (dest_r, dest_g, dest_b) = from_rgb32(palette[dest]);
let [dest_r, dest_g, dest_b] = from_rgb32(palette[dest]);
let find_r = lerp(dest_r as f32, source_r as f32, blend_r) as u8;
let find_g = lerp(dest_g as f32, source_g as f32, blend_g) as u8;

View file

@ -59,15 +59,15 @@ impl BlendFunction {
///
/// # Arguments
///
/// * `a`: the alpha component (0-255)
/// * `r`: the red component (0-255)
/// * `g`: the green component (0-255)
/// * `b`: the blue component (0-255)
/// * `argb` the 4 color components (0-255) in the order: alpha, red, green, blue
///
/// returns: the u32 packed color
#[inline]
pub fn to_argb32(a: u8, r: u8, g: u8, b: u8) -> u32 {
(b as u32) + ((g as u32) << 8) + ((r as u32) << 16) + ((a as u32) << 24)
pub fn to_argb32(argb: [u8; 4]) -> u32 {
(argb[3] as u32) // b
+ ((argb[2] as u32) << 8) // g
+ ((argb[1] as u32) << 16) // r
+ ((argb[0] as u32) << 24) // a
}
/// Converts a set of individual ARGB normalized components to a combined 32-bit color value,
@ -75,18 +75,15 @@ pub fn to_argb32(a: u8, r: u8, g: u8, b: u8) -> u32 {
///
/// # Arguments
///
/// * `a`: the normalized alpha component (0.0 to 1.0)
/// * `r`: the normalized red component (0.0 to 1.0)
/// * `g`: the normalized green component (0.0 to 1.0)
/// * `b`: the normalized blue component (0.0 to 1.0)
/// * `argb` the 4 normalized color components (0.0 to 1.0) in the order: alpha, red, green, blue
///
/// returns: the u32 packed color
#[inline]
pub fn to_argb32_normalized(a: f32, r: f32, g: f32, b: f32) -> u32 {
(((b * 255.0) as u32) & 0xff)
+ ((((g * 255.0) as u32) & 0xff) << 8)
+ ((((r * 255.0) as u32) & 0xff) << 16)
+ ((((a * 255.0) as u32) & 0xff) << 24)
pub fn to_argb32_normalized(argb: [f32; 4]) -> u32 {
(((argb[3] * 255.0) as u32) & 0xff) // b
+ ((((argb[2] * 255.0) as u32) & 0xff) << 8) // g
+ ((((argb[1] * 255.0) as u32) & 0xff) << 16) // r
+ ((((argb[0] * 255.0) as u32) & 0xff) << 24) // a
}
/// Extracts the individual ARGB components out of a combined 32-bit color value which is in the
@ -98,12 +95,13 @@ pub fn to_argb32_normalized(a: f32, r: f32, g: f32, b: f32) -> u32 {
///
/// returns: the individual ARGB color components (0-255 each) in order: alpha, red, green, blue
#[inline]
pub fn from_argb32(argb: u32) -> (u8, u8, u8, u8) {
let a = ((argb & 0xff000000) >> 24) as u8;
let r = ((argb & 0x00ff0000) >> 16) as u8;
let g = ((argb & 0x0000ff00) >> 8) as u8;
let b = (argb & 0x000000ff) as u8;
(a, r, g, b)
pub fn from_argb32(argb: u32) -> [u8; 4] {
[
((argb & 0xff000000) >> 24) as u8, // a
((argb & 0x00ff0000) >> 16) as u8, // r
((argb & 0x0000ff00) >> 8) as u8, // g
(argb & 0x000000ff) as u8, // b
]
}
/// Extracts the individual ARGB normalized components out of a combined 32-bit color value which
@ -115,12 +113,13 @@ pub fn from_argb32(argb: u32) -> (u8, u8, u8, u8) {
///
/// returns: the individual ARGB normalized color components (0.0 to 1.0 each) in order: alpha, red, green, blue
#[inline]
pub fn from_argb32_normalized(argb: u32) -> (f32, f32, f32, f32) {
let a = ((argb & 0xff000000) >> 24) as f32 / 255.0;
let r = ((argb & 0x00ff0000) >> 16) as f32 / 255.0;
let g = ((argb & 0x0000ff00) >> 8) as f32 / 255.0;
let b = (argb & 0x000000ff) as f32 / 255.0;
(a, r, g, b)
pub fn from_argb32_normalized(argb: u32) -> [f32; 4] {
[
((argb & 0xff000000) >> 24) as f32 / 255.0, // a
((argb & 0x00ff0000) >> 16) as f32 / 255.0, // r
((argb & 0x0000ff00) >> 8) as f32 / 255.0, // g
(argb & 0x000000ff) as f32 / 255.0, // b
]
}
/// Converts a set of individual RGB components to a combined 32-bit color value, packed into
@ -128,14 +127,12 @@ pub fn from_argb32_normalized(argb: u32) -> (f32, f32, f32, f32) {
///
/// # Arguments
///
/// * `r`: the red component (0-255)
/// * `g`: the green component (0-255)
/// * `b`: the blue component (0-255)
/// * `rgb` the 3 color components (0-255) in the order: red, green, blue
///
/// returns: the u32 packed color
#[inline]
pub fn to_rgb32(r: u8, g: u8, b: u8) -> u32 {
to_argb32(255, r, g, b)
pub fn to_rgb32(rgb: [u8; 3]) -> u32 {
to_argb32([255, rgb[0], rgb[1], rgb[2]])
}
/// Converts a set of individual RGB normalized components to a combined 32-bit color value, packed
@ -143,14 +140,12 @@ pub fn to_rgb32(r: u8, g: u8, b: u8) -> u32 {
///
/// # Arguments
///
/// * `r`: the normalized red component (0.0 to 1.0)
/// * `g`: the normalized green component (0.0 to 1.0)
/// * `b`: the normalized blue component (0.0 to 1.0)
/// * `rgb` the 3 normalized color components (0.0 to 1.0) in the order: red, green, blue
///
/// returns: the u32 packed color
#[inline]
pub fn to_rgb32_normalized(r: f32, g: f32, b: f32) -> u32 {
to_argb32_normalized(1.0, r, g, b)
pub fn to_rgb32_normalized(rgb: [f32; 3]) -> u32 {
to_argb32_normalized([1.0, rgb[0], rgb[1], rgb[2]])
}
/// Extracts the individual RGB components out of a combined 32-bit color value which is in the
@ -162,12 +157,13 @@ pub fn to_rgb32_normalized(r: f32, g: f32, b: f32) -> u32 {
///
/// returns: the individual ARGB color components (0-255 each) in order: red, green, blue
#[inline]
pub fn from_rgb32(rgb: u32) -> (u8, u8, u8) {
pub fn from_rgb32(rgb: u32) -> [u8; 3] {
// ignore alpha component at 0xff000000 ...
let r = ((rgb & 0x00ff0000) >> 16) as u8;
let g = ((rgb & 0x0000ff00) >> 8) as u8;
let b = (rgb & 0x000000ff) as u8;
(r, g, b)
[
((rgb & 0x00ff0000) >> 16) as u8, // r
((rgb & 0x0000ff00) >> 8) as u8, // g
(rgb & 0x000000ff) as u8, // b
]
}
/// Extracts the individual RGB normalized components out of a combined 32-bit color value which
@ -179,12 +175,13 @@ pub fn from_rgb32(rgb: u32) -> (u8, u8, u8) {
///
/// returns: the individual ARGB normalized color components (0.0 to 1.0 each) in order: red, green, blue
#[inline]
pub fn from_rgb32_normalized(rgb: u32) -> (f32, f32, f32) {
pub fn from_rgb32_normalized(rgb: u32) -> [f32; 3] {
// ignore alpha component at 0xff000000 ...
let r = ((rgb & 0x00ff0000) >> 16) as f32 / 255.0;
let g = ((rgb & 0x0000ff00) >> 8) as f32 / 255.0;
let b = (rgb & 0x000000ff) as f32 / 255.0;
(r, g, b)
[
((rgb & 0x00ff0000) >> 16) as f32 / 255.0, // r
((rgb & 0x0000ff00) >> 8) as f32 / 255.0, // g
(rgb & 0x000000ff) as f32 / 255.0, // b
]
}
/// Blends two color components together using a "strength" factor to control how much of the source
@ -216,14 +213,14 @@ pub fn blend_components(strength: u8, src: u8, dest: u8) -> u8 {
/// returns: the blended result
#[inline]
pub fn blend_argb32(src: u32, dest: u32) -> u32 {
let (src_a, src_r, src_g, src_b) = from_argb32(src);
let (dest_a, dest_r, dest_g, dest_b) = from_argb32(dest);
to_argb32(
let [src_a, src_r, src_g, src_b] = from_argb32(src);
let [dest_a, dest_r, dest_g, dest_b] = from_argb32(dest);
to_argb32([
blend_components(src_a, src_a, dest_a),
blend_components(src_a, src_r, dest_r),
blend_components(src_a, src_g, dest_g),
blend_components(src_a, src_b, dest_b),
)
])
}
/// Blends the source and destination colors together, where the alpha value used to blend the two
@ -245,15 +242,15 @@ pub fn blend_argb32(src: u32, dest: u32) -> u32 {
/// returns: the blended result
#[inline]
pub fn blend_argb32_source_by(src: u32, dest: u32, alpha: u8) -> u32 {
let (src_a, src_r, src_g, src_b) = from_argb32(src);
let (dest_r, dest_g, dest_b) = from_rgb32(dest);
let [src_a, src_r, src_g, src_b] = from_argb32(src);
let [dest_r, dest_g, dest_b] = from_rgb32(dest);
let alpha = ((alpha as u16 * src_a as u16) / 255) as u8;
to_argb32(
to_argb32([
alpha,
blend_components(alpha, src_r, dest_r),
blend_components(alpha, src_g, dest_g),
blend_components(alpha, src_b, dest_b),
)
])
}
/// Applies a tint to a color, using the tint color's alpha component as the strength of the tint,
@ -269,14 +266,14 @@ pub fn blend_argb32_source_by(src: u32, dest: u32, alpha: u8) -> u32 {
/// returns: the resulting tinted color
#[inline]
pub fn tint_argb32(color: u32, tint: u32) -> u32 {
let (color_a, color_r, color_g, color_b) = from_argb32(color);
let (tint_a, tint_r, tint_g, tint_b) = from_argb32(tint);
to_argb32(
let [color_a, color_r, color_g, color_b] = from_argb32(color);
let [tint_a, tint_r, tint_g, tint_b] = from_argb32(tint);
to_argb32([
color_a,
blend_components(tint_a, tint_r, color_r),
blend_components(tint_a, tint_g, color_g),
blend_components(tint_a, tint_b, color_b),
)
])
}
/// Multiplies two colors together, returing the result. The multiplication is performed by
@ -291,14 +288,14 @@ pub fn tint_argb32(color: u32, tint: u32) -> u32 {
/// returns: the resulting color from the multiplication
#[inline]
pub fn multiply_argb32(a: u32, b: u32) -> u32 {
let (a_a, a_r, a_g, a_b) = from_argb32(a);
let (b_a, b_r, b_g, b_b) = from_argb32(b);
to_argb32(
let [a_a, a_r, a_g, a_b] = from_argb32(a);
let [b_a, b_r, b_g, b_b] = from_argb32(b);
to_argb32([
((a_a as u32 * b_a as u32) / 255) as u8,
((a_r as u32 * b_r as u32) / 255) as u8,
((a_g as u32 * b_g as u32) / 255) as u8,
((a_b as u32 * b_b as u32) / 255) as u8,
)
])
}
/// Linearly interpolates between two 32-bit packed colors in the format 0xAARRGGBB.
@ -310,14 +307,14 @@ pub fn multiply_argb32(a: u32, b: u32) -> u32 {
/// * `t`: the amount to interpolate between the two values, specified as a fraction.
#[inline]
pub fn lerp_argb32(a: u32, b: u32, t: f32) -> u32 {
let (a1, r1, g1, b1) = from_argb32(a);
let (a2, r2, g2, b2) = from_argb32(b);
to_argb32(
let [a1, r1, g1, b1] = from_argb32(a);
let [a2, r2, g2, b2] = from_argb32(b);
to_argb32([
((a1 as f32) + ((a2 as f32) - (a1 as f32)) * t) as u8,
((r1 as f32) + ((r2 as f32) - (r1 as f32)) * t) as u8,
((g1 as f32) + ((g2 as f32) - (g1 as f32)) * t) as u8,
((b1 as f32) + ((b2 as f32) - (b1 as f32)) * t) as u8,
)
])
}
/// Linearly interpolates between two 32-bit packed colors in the format 0xAARRGGBB. Ignores the
@ -330,13 +327,13 @@ pub fn lerp_argb32(a: u32, b: u32, t: f32) -> u32 {
/// * `t`: the amount to interpolate between the two values, specified as a fraction.
#[inline]
pub fn lerp_rgb32(a: u32, b: u32, t: f32) -> u32 {
let (r1, g1, b1) = from_rgb32(a);
let (r2, g2, b2) = from_rgb32(b);
to_rgb32(
let [r1, g1, b1] = from_rgb32(a);
let [r2, g2, b2] = from_rgb32(b);
to_rgb32([
((r1 as f32) + ((r2 as f32) - (r1 as f32)) * t) as u8,
((g1 as f32) + ((g2 as f32) - (g1 as f32)) * t) as u8,
((b1 as f32) + ((b2 as f32) - (b1 as f32)) * t) as u8,
)
])
}
const LUMINANCE_RED: f32 = 0.212655;
@ -353,10 +350,10 @@ fn srgb_to_linearized(color_channel: u8) -> f32 {
}
/// Calculates the given sRGB color's luminance, returned as a value between 0.0 and 1.0.
pub fn luminance(r: u8, g: u8, b: u8) -> f32 {
(LUMINANCE_RED * srgb_to_linearized(r))
+ (LUMINANCE_GREEN * srgb_to_linearized(g))
+ (LUMINANCE_BLUE * srgb_to_linearized(b))
pub fn luminance(rgb: [u8; 3]) -> f32 {
(LUMINANCE_RED * srgb_to_linearized(rgb[0]))
+ (LUMINANCE_GREEN * srgb_to_linearized(rgb[1]))
+ (LUMINANCE_BLUE * srgb_to_linearized(rgb[2]))
}
fn brightness(mut luminance: f32) -> f32 {
@ -370,8 +367,8 @@ fn brightness(mut luminance: f32) -> f32 {
/// Calculates the approximate "brightness" / grey-scale value for the given sRGB color, returned
/// as a value between 0 and 255.
pub fn greyscale(r: u8, b: u8, g: u8) -> u8 {
(brightness(luminance(r, g, b)) * 255.0) as u8
pub fn greyscale(rgb: [u8; 3]) -> u8 {
(brightness(luminance(rgb)) * 255.0) as u8
}
#[cfg(test)]
@ -381,19 +378,19 @@ mod tests {
#[test]
fn argb_conversions() {
let argb = to_argb32(0x11, 0x22, 0x33, 0x44);
let argb = to_argb32([0x11, 0x22, 0x33, 0x44]);
assert_eq!(argb, 0x11223344);
let argb = to_rgb32(0x22, 0x33, 0x44);
let argb = to_rgb32([0x22, 0x33, 0x44]);
assert_eq!(argb, 0xff223344);
let (a, r, g, b) = from_argb32(0x11223344);
let [a, r, g, b] = from_argb32(0x11223344);
assert_eq!(0x11, a);
assert_eq!(0x22, r);
assert_eq!(0x33, g);
assert_eq!(0x44, b);
let (r, g, b) = from_rgb32(0x11223344);
let [r, g, b] = from_rgb32(0x11223344);
assert_eq!(0x22, r);
assert_eq!(0x33, g);
assert_eq!(0x44, b);
@ -401,22 +398,22 @@ mod tests {
#[test]
fn normalized_argb_conversions() {
let argb = to_argb32_normalized(0.5, 0.1, 0.2, 0.3);
let argb = to_argb32_normalized([0.5, 0.1, 0.2, 0.3]);
assert_eq!(argb, 0x7f19334c);
let argb = to_rgb32_normalized(0.1, 0.2, 0.3);
let argb = to_rgb32_normalized([0.1, 0.2, 0.3]);
assert_eq!(argb, 0xff19334c);
// floating-point accuracy is a real bitch here ... lol.
// the low-accuracy epsilon values in these asserts is not an accident or oversight
let (a, r, g, b) = from_argb32_normalized(0x7f19334c);
let [a, r, g, b] = from_argb32_normalized(0x7f19334c);
assert!(a.nearly_equal(0.5, 0.01));
assert!(r.nearly_equal(0.1, 0.01));
assert!(g.nearly_equal(0.2, 0.01));
assert!(b.nearly_equal(0.3, 0.01));
let (r, g, b) = from_rgb32_normalized(0x7f19334c);
let [r, g, b] = from_rgb32_normalized(0x7f19334c);
assert!(r.nearly_equal(0.1, 0.01));
assert!(g.nearly_equal(0.2, 0.01));
assert!(b.nearly_equal(0.3, 0.01));

View file

@ -33,12 +33,12 @@ fn read_palette_6bit<T: ReadBytesExt>(reader: &mut T, num_colors: usize) -> Resu
if num_colors > NUM_COLORS {
return Err(PaletteError::OutOfRange(num_colors));
}
let mut colors = [to_argb32(255, 0, 0, 0); NUM_COLORS];
let mut colors = [to_argb32([255, 0, 0, 0]); NUM_COLORS];
for i in 0..num_colors {
let r = reader.read_u8()?;
let g = reader.read_u8()?;
let b = reader.read_u8()?;
let color = to_rgb32(from_6bit(r), from_6bit(g), from_6bit(b));
let color = to_rgb32([from_6bit(r), from_6bit(g), from_6bit(b)]);
colors[i] = color;
}
Ok(colors)
@ -53,7 +53,7 @@ fn write_palette_6bit<T: WriteBytesExt>(
return Err(PaletteError::OutOfRange(num_colors));
}
for i in 0..num_colors {
let (r, g, b) = from_rgb32(colors[i]);
let [r, g, b] = from_rgb32(colors[i]);
writer.write_u8(to_6bit(r))?;
writer.write_u8(to_6bit(g))?;
writer.write_u8(to_6bit(b))?;
@ -66,12 +66,12 @@ fn read_palette_8bit<T: ReadBytesExt>(reader: &mut T, num_colors: usize) -> Resu
if num_colors > NUM_COLORS {
return Err(PaletteError::OutOfRange(num_colors));
}
let mut colors = [to_argb32(255, 0, 0, 0); NUM_COLORS];
let mut colors = [to_argb32([255, 0, 0, 0]); NUM_COLORS];
for i in 0..num_colors {
let r = reader.read_u8()?;
let g = reader.read_u8()?;
let b = reader.read_u8()?;
let color = to_rgb32(r, g, b);
let color = to_rgb32([r, g, b]);
colors[i] = color;
}
Ok(colors)
@ -86,7 +86,7 @@ fn write_palette_8bit<T: WriteBytesExt>(
return Err(PaletteError::OutOfRange(num_colors));
}
for i in 0..num_colors {
let (r, g, b) = from_rgb32(colors[i]);
let [r, g, b] = from_rgb32(colors[i]);
writer.write_u8(r)?;
writer.write_u8(g)?;
writer.write_u8(b)?;
@ -125,7 +125,7 @@ impl Palette {
/// Creates a new Palette with all initial colors having the RGB values specified.
pub fn new_with_default(r: u8, g: u8, b: u8) -> Palette {
let rgb = to_rgb32(r, g, b);
let rgb = to_rgb32([r, g, b]);
Palette { colors: [rgb; NUM_COLORS] }
}
@ -295,7 +295,7 @@ impl Palette {
pub fn fade_color_toward_rgb(&mut self, color: u8, target_r: u8, target_g: u8, target_b: u8, step: u8) -> bool {
let mut modified = false;
let (mut r, mut g, mut b) = from_rgb32(self.colors[color as usize]);
let [mut r, mut g, mut b] = from_rgb32(self.colors[color as usize]);
if r != target_r {
modified = true;
@ -328,7 +328,7 @@ impl Palette {
}
if modified {
self.colors[color as usize] = to_rgb32(r, g, b);
self.colors[color as usize] = to_rgb32([r, g, b]);
}
(target_r == r) && (target_g == g) && (target_b == b)
@ -381,7 +381,7 @@ impl Palette {
pub fn fade_colors_toward_palette<T: ColorRange>(&mut self, colors: T, palette: &Palette, step: u8) -> bool {
let mut all_faded = true;
for color in colors {
let (r, g, b) = from_rgb32(palette[color]);
let [r, g, b] = from_rgb32(palette[color]);
if !self.fade_color_toward_rgb(color, r, g, b, step) {
all_faded = false;
}
@ -439,7 +439,7 @@ impl Palette {
let mut closest = 0;
for (index, color) in self.colors.iter().enumerate() {
let (this_r, this_g, this_b) = from_rgb32(*color);
let [this_r, this_g, this_b] = from_rgb32(*color);
if r == this_r && g == this_g && b == this_b {
return index as u8;

View file

@ -61,8 +61,8 @@ fn setup_for_blending_half_solid_half_semi_transparent() -> RgbaBitmap {
for x in 0..screen.width() {
unsafe {
let pixel = screen.get_pixel_unchecked(x as i32, y as i32);
let (r, g, b) = from_rgb32(pixel);
screen.set_pixel_unchecked(x as i32, y as i32, to_argb32(127, r, g, b));
let [r, g, b] = from_rgb32(pixel);
screen.set_pixel_unchecked(x as i32, y as i32, to_argb32([127, r, g, b]));
}
}
}
@ -83,7 +83,7 @@ fn pixel_addressing() {
let mut i = 0;
for _y in 0..16 {
for _x in 0..16 {
*pixels = to_rgb32(i, i, i);
*pixels = to_rgb32([i, i, i]);
i = i.wrapping_add(1);
pixels = pixels.offset(1);
}
@ -619,7 +619,7 @@ fn generate_solid_bitmap_with_varied_alpha(width: i32, height: i32) -> RgbaBitma
let y_third = height / 3;
let mut bitmap = RgbaBitmap::new(width as u32, height as u32).unwrap();
bitmap.clear(to_argb32(255, 0, 0, 0));
bitmap.clear(to_argb32([255, 0, 0, 0]));
bitmap.filled_rect(0, 0, x_third, y_third, 0x330000aa);
bitmap.filled_rect(x_third * 2 + 1, y_third * 2 + 1, width - 1, height - 1, 0x6600aa00);
@ -712,7 +712,7 @@ fn solid_tinted_blits() {
let bmp21 = generate_bitmap(21, 21);
let bmp3 = generate_bitmap(3, 3);
let method = SolidTinted(to_argb32(127, 155, 242, 21));
let method = SolidTinted(to_argb32([127, 155, 242, 21]));
let x = 40;
let y = 20;
@ -911,7 +911,7 @@ fn solid_flipped_tinted_blits() {
let bmp = generate_bitmap(16, 16);
let tint_color = to_argb32(127, 155, 242, 21);
let tint_color = to_argb32([127, 155, 242, 21]);
let x = 40;
let y = 20;
@ -1046,7 +1046,7 @@ fn transparent_blits() {
let bmp21 = generate_bitmap(21, 21);
let bmp3 = generate_bitmap(3, 3);
let method = Transparent(to_rgb32(0, 0, 0));
let method = Transparent(to_rgb32([0, 0, 0]));
let x = 40;
let y = 20;
@ -1115,7 +1115,8 @@ fn transparent_tinted_blits() {
let bmp21 = generate_bitmap(21, 21);
let bmp3 = generate_bitmap(3, 3);
let method = TransparentTinted { transparent_color: to_rgb32(0, 0, 0), tint_color: to_argb32(127, 155, 242, 21) };
let method =
TransparentTinted { transparent_color: to_rgb32([0, 0, 0]), tint_color: to_argb32([127, 155, 242, 21]) };
let x = 40;
let y = 20;
@ -1183,7 +1184,7 @@ fn blended_transparent_blits() {
let bmp21 = generate_solid_bitmap_with_varied_alpha(21, 21);
let bmp3 = generate_solid_bitmap_with_varied_alpha(3, 3);
let method = TransparentBlended { transparent_color: to_argb32(255, 0, 0, 0), blend: BlendFunction::Blend };
let method = TransparentBlended { transparent_color: to_argb32([255, 0, 0, 0]), blend: BlendFunction::Blend };
let x = 40;
let y = 20;
@ -1248,7 +1249,7 @@ fn transparent_flipped_blits() {
let mut screen = setup();
screen.clear(LIGHTER_BACKGROUND);
let transparent_color = to_rgb32(0, 0, 0);
let transparent_color = to_rgb32([0, 0, 0]);
let bmp = generate_bitmap(16, 16);
@ -1315,8 +1316,8 @@ fn transparent_flipped_tinted_blits() {
let mut screen = setup();
screen.clear(LIGHTER_BACKGROUND);
let transparent_color = to_rgb32(0, 0, 0);
let tint_color = to_argb32(127, 155, 242, 21);
let transparent_color = to_rgb32([0, 0, 0]);
let tint_color = to_argb32([127, 155, 242, 21]);
let bmp = generate_bitmap(16, 16);
@ -1384,7 +1385,7 @@ fn blended_transparent_flipped_blits() {
let bmp = generate_solid_bitmap_with_varied_alpha(16, 16);
let transparent_color = to_argb32(255, 0, 0, 0);
let transparent_color = to_argb32([255, 0, 0, 0]);
let blend = BlendFunction::Blend;
let x = 40;
@ -1450,7 +1451,7 @@ fn transparent_single_blits() {
let mut screen = setup();
screen.clear(LIGHTER_BACKGROUND);
let transparent_color = to_rgb32(0, 0, 0);
let transparent_color = to_rgb32([0, 0, 0]);
let bmp = generate_bitmap(16, 16);
@ -1521,7 +1522,7 @@ fn transparent_flipped_single_blits() {
let mut screen = setup();
screen.clear(LIGHTER_BACKGROUND);
let transparent_color = to_rgb32(0, 0, 0);
let transparent_color = to_rgb32([0, 0, 0]);
let bmp = generate_bitmap(16, 16);
@ -1656,7 +1657,7 @@ fn rotozoom_tinted_blits() {
let bmp = generate_bitmap(16, 16);
let tint_color = to_argb32(127, 155, 242, 21);
let tint_color = to_argb32([127, 155, 242, 21]);
let x = 40;
let y = 20;
@ -1790,7 +1791,7 @@ fn rotozoom_transparent_blits() {
let mut screen = setup();
screen.clear(LIGHTER_BACKGROUND);
let transparent_color = to_rgb32(0, 0, 0);
let transparent_color = to_rgb32([0, 0, 0]);
let bmp = generate_bitmap(16, 16);
@ -1859,8 +1860,8 @@ fn rotozoom_transparent_tinted_blits() {
let mut screen = setup();
screen.clear(LIGHTER_BACKGROUND);
let transparent_color = to_rgb32(0, 0, 0);
let tint_color = to_argb32(127, 155, 242, 21);
let transparent_color = to_rgb32([0, 0, 0]);
let tint_color = to_argb32([127, 155, 242, 21]);
let bmp = generate_bitmap(16, 16);
@ -1930,7 +1931,7 @@ fn blended_rotozoom_transparent_blits() {
let bmp = generate_solid_bitmap_with_varied_alpha(16, 16);
let transparent_color = to_argb32(255, 0, 0, 0);
let transparent_color = to_argb32([255, 0, 0, 0]);
let blend = BlendFunction::Blend;
let x = 40;
@ -2023,34 +2024,34 @@ fn blend_function_tinted_blend() {
let bmp_solid_with_varied_alpha = generate_solid_bitmap_with_varied_alpha(32, 32);
let bmp_with_varied_alpha = generate_bitmap_with_varied_alpha(32, 32);
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32(255, 155, 242, 21)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32([255, 155, 242, 21])));
screen.blit(method.clone(), &bmp_solid, 10, 5);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 5);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 5);
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32(127, 155, 242, 21)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32([127, 155, 242, 21])));
screen.blit(method.clone(), &bmp_solid, 10, 40);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 40);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 40);
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32(0, 155, 242, 21)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32([0, 155, 242, 21])));
screen.blit(method.clone(), &bmp_solid, 10, 75);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 75);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 75);
//////
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32(255, 155, 242, 21)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32([255, 155, 242, 21])));
screen.blit(method.clone(), &bmp_solid, 10, 125);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 125);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 125);
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32(127, 155, 242, 21)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32([127, 155, 242, 21])));
screen.blit(method.clone(), &bmp_solid, 10, 160);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 160);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 160);
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32(0, 155, 242, 21)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::TintedBlend(to_argb32([0, 155, 242, 21])));
screen.blit(method.clone(), &bmp_solid, 10, 195);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 195);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 195);
@ -2113,34 +2114,34 @@ fn blend_function_multiplied_blend() {
let bmp_solid_with_varied_alpha = generate_solid_bitmap_with_varied_alpha(32, 32);
let bmp_with_varied_alpha = generate_bitmap_with_varied_alpha(32, 32);
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32(255, 242, 29, 81)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32([255, 242, 29, 81])));
screen.blit(method.clone(), &bmp_solid, 10, 5);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 5);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 5);
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32(127, 242, 29, 81)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32([127, 242, 29, 81])));
screen.blit(method.clone(), &bmp_solid, 10, 40);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 40);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 40);
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32(0, 242, 29, 81)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32([0, 242, 29, 81])));
screen.blit(method.clone(), &bmp_solid, 10, 75);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 75);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 75);
//////
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32(255, 242, 29, 81)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32([255, 242, 29, 81])));
screen.blit(method.clone(), &bmp_solid, 10, 125);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 125);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 125);
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32(127, 242, 29, 81)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32([127, 242, 29, 81])));
screen.blit(method.clone(), &bmp_solid, 10, 160);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 160);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 160);
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32(0, 242, 29, 81)));
let method = RgbaBlitMethod::SolidBlended(BlendFunction::MultipliedBlend(to_argb32([0, 242, 29, 81])));
screen.blit(method.clone(), &bmp_solid, 10, 195);
screen.blit(method.clone(), &bmp_solid_with_varied_alpha, 100, 195);
screen.blit(method.clone(), &bmp_with_varied_alpha, 200, 195);
@ -2395,15 +2396,15 @@ fn get_quad(
let positions_2 = [top_left, bottom_right, top_right];
let texcoords_1 = [Vector2::new(0.0, 0.0), Vector2::new(0.0, 1.0), Vector2::new(1.0, 1.0)];
let texcoords_2 = [Vector2::new(0.0, 0.0), Vector2::new(1.0, 1.0), Vector2::new(1.0, 0.0)];
let single_color = to_argb32(128, 255, 0, 255);
let colors_1 = [to_rgb32(255, 0, 0), to_rgb32(0, 255, 0), to_rgb32(0, 0, 255)];
let colors_2 = [to_rgb32(255, 0, 0), to_rgb32(0, 0, 255), to_rgb32(255, 255, 255)];
let tint_color = to_argb32(128, 192, 47, 160);
let single_color = to_argb32([128, 255, 0, 255]);
let colors_1 = [to_rgb32([255, 0, 0]), to_rgb32([0, 255, 0]), to_rgb32([0, 0, 255])];
let colors_2 = [to_rgb32([255, 0, 0]), to_rgb32([0, 0, 255]), to_rgb32([255, 255, 255])];
let tint_color = to_argb32([128, 192, 47, 160]);
match mode {
TriangleType::Solid => [
RgbaTriangle2d::Solid { position: positions_1, color: to_rgb32(255, 0, 255) },
RgbaTriangle2d::Solid { position: positions_2, color: to_rgb32(255, 0, 255) },
RgbaTriangle2d::Solid { position: positions_1, color: to_rgb32([255, 0, 255]) },
RgbaTriangle2d::Solid { position: positions_2, color: to_rgb32([255, 0, 255]) },
],
TriangleType::SolidBlended => [
RgbaTriangle2d::SolidBlended { position: positions_1, color: single_color, blend: BlendFunction::Blend },

View file

@ -87,7 +87,7 @@ fn keyboard_state() {
display_key_state(*key, &mut system, 2, 160 + (idx as i32 * 10));
}
system.res.video.set_pixel(system.res.mouse.x(), system.res.mouse.y(), to_rgb32(255, 0, 255));
system.res.video.set_pixel(system.res.mouse.x(), system.res.mouse.y(), to_rgb32([255, 0, 255]));
system.display().unwrap();
}

View file

@ -62,7 +62,7 @@ fn mouse_with_custom_cursor() {
system.update().unwrap();
display_mouse_state(&mut system);
system.res.video.set_pixel(system.res.mouse.x(), system.res.mouse.y(), to_rgb32(255, 0, 255));
system.res.video.set_pixel(system.res.mouse.x(), system.res.mouse.y(), to_rgb32([255, 0, 255]));
system.display().unwrap();
}
@ -91,7 +91,7 @@ fn mouse_with_os_cursor() {
system.update().unwrap();
display_mouse_state(&mut system);
system.res.video.set_pixel(system.res.mouse.x(), system.res.mouse.y(), to_rgb32(255, 0, 255));
system.res.video.set_pixel(system.res.mouse.x(), system.res.mouse.y(), to_rgb32([255, 0, 255]));
system.display().unwrap();
}

View file

@ -40,7 +40,7 @@ fn system_events_display() {
&format!("{:?}", event),
2,
system.res.video.height() as i32 - 10 - (idx as i32 * 10),
FontRenderOpts::Color(to_rgb32(255, 255, 255)),
FontRenderOpts::Color(to_rgb32([255, 255, 255])),
&system.res.font,
);
}

View file

@ -27,8 +27,8 @@ fn simple_main_loop(mut system: System<Standard>) {
draw_base_screen(
&mut system.res.video,
to_rgb32(32, 32, 32),
to_rgb32(44, 44, 44),
to_rgb32([32, 32, 32]),
to_rgb32([44, 44, 44]),
COLOR_BRIGHT_WHITE,
COLOR_BRIGHT_RED,
);

View file

@ -67,9 +67,9 @@ impl Renderer {
Vector2::new(v3.uv[0], v3.uv[1]),
],
&[
to_argb32(v2.col[3], v2.col[0], v2.col[1], v2.col[2]),
to_argb32(v1.col[3], v1.col[0], v1.col[1], v1.col[2]),
to_argb32(v3.col[3], v3.col[0], v3.col[1], v3.col[2]),
to_argb32([v2.col[3], v2.col[0], v2.col[1], v2.col[2]]),
to_argb32([v1.col[3], v1.col[0], v1.col[1], v1.col[2]]),
to_argb32([v3.col[3], v3.col[0], v3.col[1], v3.col[2]]),
],
bitmap,
BlendFunction::Blend,