Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color convertion methods #167

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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