Skip to content

How to change color? #58

Open
Open

Description

@karimpanacci

It seems that tinybmp automatically decides the color of the image, how do I change it?
I'm using BinaryColor and I'd like to reverse the colors

Activity

  1. rfuest commented on Jul 27, 2026

    @rfuest
    Member

    I don't know what you mean by "automatically decides",tinybmp should display the image like it is stored inside the BMP file. Do you experience a random selection of colors (like some images are inverted and some are not)? There have been some bugs related to this in the past, but I think they should be fixed in current releases.

    BinaryColor is a bit of a special case, because BinaryColor::On can be a light pixel (e.g. on an OLED) or a dark pixel (on a LCD). tinybmp maps white pixels to BinaryColor::On and black pixels to BinaryColor::Off.

    If you can give me a few more details about your application, like the type of display you are using, and an example BMP file, that isn't drawn like you expect, I will be able to give you a better answer.

  2. karimpanacci commented on Jul 27, 2026

    @karimpanacci
    Author

    @rfuest Yes I was referring only to BinaryColor. I’m using an e-paper display and have set the “On” color to black.

    So when I view images with tinybmp, the colors appear reversed.

    It would be convenient to be able to choose this behavior.

  3. rfuest commented on Jul 27, 2026

    @rfuest
    Member

    I see what you mean and I agree that being able to invert the image for LCDs and EPDs is a useful feature. But I need to think about if I want to include this feature in tinybmp itself.

    In the meantime you can use an embedded-graphics DrawTarget adapter to invert all embedded-graphics operations:

    pub struct InvertedDrawTarget<'a, T: DrawTarget<Color = BinaryColor>> {
        target: &'a mut T,
    }
    
    impl<'a, T: DrawTarget<Color = BinaryColor>> InvertedDrawTarget<'a, T> {
        pub fn new(target: &'a mut T) -> Self {
            Self { target }
        }
    }
    
    impl<T: DrawTarget<Color = BinaryColor> + OriginDimensions> DrawTarget
        for InvertedDrawTarget<'_, T>
    {
        type Color = BinaryColor;
        type Error = T::Error;
    
        fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
        where
            I: IntoIterator<Item = Pixel<Self::Color>>,
        {
            self.target
                .draw_iter(pixels.into_iter().map(|Pixel(p, c)| Pixel(p, c.invert())))
        }
    
        fn fill_contiguous<I>(
            &mut self,
            area: &embedded_graphics::primitives::Rectangle,
            colors: I,
        ) -> Result<(), Self::Error>
        where
            I: IntoIterator<Item = Self::Color>,
        {
            self.target
                .fill_contiguous(area, colors.into_iter().map(|c| c.invert()))
        }
    
        fn fill_solid(
            &mut self,
            area: &embedded_graphics::primitives::Rectangle,
            color: Self::Color,
        ) -> Result<(), Self::Error> {
            self.target.fill_solid(area, color.invert())
        }
    
        fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
            self.target.clear(color.invert())
        }
    }
    
    impl<T: DrawTarget<Color = BinaryColor> + OriginDimensions> OriginDimensions
        for InvertedDrawTarget<'_, T>
    {
        fn size(&self) -> Size {
            self.target.size()
        }
    }

    It can be used like this:

    let mut display = SomeDisplay::new(...);
    
    let bmp = Bmp::from_slice(&BMP_DATA).unwrap();
    Image::new(&bmp, Point::zero()).draw(&mut InvertedDrawTarget::new(&mut display)).unwrap();
  4. rfuest commented on Aug 13, 2026

    @rfuest
    Member

    @karimpanacci: Did you try the DrawTarget adapter based solution? If that works for you I would prefer to add something similar to the next embedded-graphics release instead of adding color inversion to tinybmp.

  5. karimpanacci commented on Aug 13, 2026

    @karimpanacci
    Author

    @rfuest Hi, I’ve been quite busy of late and forgot to try it out; I’ve just tested it now on the code I was working on and it works.

    The only issue is that InvertedTarget required both DrawTarget and OriginDimensions, but I was working on some code that simply had an impl DrawTarget as its input.

    So, to use it without having to change all my code, I had to remove OriginDimensions as a constraint.

    And I implemented it like this:

    impl<T: DrawTarget<Color = BinaryColor>> OriginDimensions for InvertedDrawTarget<'_, T> {
        fn size(&self) -> Size {
            self.target.bounding_box().size
        }
    }

    I’m not sure if that’s correct, because I’ve read that OriginDimensions should be used when the origin is always 0, but I couldn’t change all the code to include OriginDimensions as a constraint as well.

    However, it worked – the image color was inverted correctly and everything worked fine.

    This is the full code I used:

    pub struct InvertedDrawTarget<'a, T: DrawTarget<Color = BinaryColor>> {
        target: &'a mut T,
    }
    
    impl<'a, T: DrawTarget<Color = BinaryColor>> InvertedDrawTarget<'a, T> {
        pub fn new(target: &'a mut T) -> Self {
            Self { target }
        }
    }
    
    impl<T: DrawTarget<Color = BinaryColor>> DrawTarget for InvertedDrawTarget<'_, T> {
        type Color = BinaryColor;
        type Error = T::Error;
    
        fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
        where
            I: IntoIterator<Item = Pixel<Self::Color>>,
        {
            self.target
                .draw_iter(pixels.into_iter().map(|Pixel(p, c)| Pixel(p, c.invert())))
        }
    
        fn fill_contiguous<I>(
            &mut self,
            area: &embedded_graphics::primitives::Rectangle,
            colors: I,
        ) -> Result<(), Self::Error>
        where
            I: IntoIterator<Item = Self::Color>,
        {
            self.target
                .fill_contiguous(area, colors.into_iter().map(|c| c.invert()))
        }
    
        fn fill_solid(
            &mut self,
            area: &embedded_graphics::primitives::Rectangle,
            color: Self::Color,
        ) -> Result<(), Self::Error> {
            self.target.fill_solid(area, color.invert())
        }
    
        fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
            self.target.clear(color.invert())
        }
    }
    
    impl<T: DrawTarget<Color = BinaryColor>> OriginDimensions for InvertedDrawTarget<'_, T> {
        fn size(&self) -> Size {
            self.target.bounding_box().size
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions