Skip to content

Commit

Permalink
Populate first draft of color crate (#3)
Browse files Browse the repository at this point in the history
This is copied from Raph's "koloro" local repository, with lots of
fiddly changes to satisfy clippy.
  • Loading branch information
raphlinus authored Nov 4, 2024
1 parent c466e12 commit 3502072
Show file tree
Hide file tree
Showing 13 changed files with 2,048 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion color/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish = false
[features]
default = ["std"]
std = []
libm = []
libm = ["dep:libm"]

[package.metadata.docs.rs]
all-features = true
Expand All @@ -25,5 +25,9 @@ targets = []

[dependencies]

[dependencies.libm]
version = "0.2.11"
optional = true

[lints]
workspace = true
50 changes: 50 additions & 0 deletions color/src/bitset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024 the Color Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! A simple bitset.
/// A simple bitset, for representing missing components.
#[derive(Default, Clone, Copy, PartialEq, Eq, Debug)]
pub struct Bitset(u8);

impl Bitset {
pub fn contains(self, ix: usize) -> bool {
(self.0 & (1 << ix)) != 0
}

pub fn set(&mut self, ix: usize) {
self.0 |= 1 << ix;
}

pub fn single(ix: usize) -> Self {
Self(1 << ix)
}

pub fn any(self) -> bool {
self.0 != 0
}
}

impl core::ops::BitAnd for Bitset {
type Output = Self;

fn bitand(self, rhs: Self) -> Self {
Self(self.0 & rhs.0)
}
}

impl core::ops::BitOr for Bitset {
type Output = Self;

fn bitor(self, rhs: Self) -> Self {
Self(self.0 | rhs.0)
}
}

impl core::ops::Not for Bitset {
type Output = Self;

fn not(self) -> Self::Output {
Self(!self.0)
}
}
Loading

0 comments on commit 3502072

Please sign in to comment.