Skip to content

Commit

Permalink
Merge pull request #167 from boozook/api/color-convert
Browse files Browse the repository at this point in the history
Add color convertion methods
  • Loading branch information
boozook authored Oct 6, 2023
2 parents 1f740fe + 7aea7d8 commit b175d2e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion api/color/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "playdate-color"
version = "0.2.1"
version = "0.2.2"
readme = "README.md"
description = "Color extension for Playdate API"
keywords = ["playdate", "sdk", "api", "gamedev"]
Expand Down
28 changes: 28 additions & 0 deletions api/color/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#![feature(impl_trait_in_assoc_type)]

extern crate sys;
use core::ptr::NonNull;
use core::usize;

use sys::error::NullPtrError;
use sys::ffi::LCDColor;
use sys::ffi::LCDPattern;
use sys::ffi::LCDSolidColor;
Expand Down Expand Up @@ -34,6 +38,30 @@ impl<'t> From<Color<'t>> for LCDColor
}
}

impl<'t> TryFrom<LCDColor> for Color<'t>
where LCDColor: 't,
Self: 't
{
type Error = NullPtrError;

fn try_from(color: LCDColor) -> Result<Self, Self::Error> {
match color {
0 => Ok(Self::Solid(LCDSolidColor::Black())),
1 => Ok(Self::Solid(LCDSolidColor::White())),
2 => Ok(Self::Solid(LCDSolidColor::Clear())),
3 => Ok(Self::Solid(LCDSolidColor::XOR())),
color => {
NonNull::new(color as *mut LCDPattern).ok_or(NullPtrError)
.map(|nn| Self::Pattern(unsafe { nn.as_ref() }))
},
}
}
}

impl<'t> From<&'t LCDPattern> for Color<'t> {
fn from(pattern: &'t LCDPattern) -> Self { Color::Pattern(pattern) }
}


#[const_trait]
pub trait LCDColorExt {
Expand Down

0 comments on commit b175d2e

Please sign in to comment.