1 个不稳定版本
0.1.0 | 2022年11月25日 |
---|
#867 in 图像
74KB
2K SLoC
Pixelar
向大家展示代码中隐藏的艺术 🧁
该像素艺术代码可以在这里找到(它是拼图中一张图片的副本)。
是什么
Pixelar 是一个包,它通过 Rust 代码提供简单的 API 来创建像素艺术。
-
该包的主要目的是为了 学习目的和将代码转化为视觉内容,这样可能更容易理解。
-
它也可以用来创建独特的像素艺术和乐趣。
依赖项
Pixelar 使用来自 image-rs 的 imageproc 和 image 来创建图像。
入门
要快速开始使用该包,只需将我们的预导入模块引入作用域,并创建一个 PixelPaper
。
use pixelar::prelude::*;
fn main() {
let mut pixel_paper = PixelPaper::<10, 10>::default();
}
注意,像素纸上常量泛型数字( ::<10, 10>
)。这些是您的纸张的高度和宽度(以像素为单位)。
这里我们有一个 10*10 的纸张。
现在您可以可变地访问纸张上的像素表并修改它。
use pixelar::prelude::*;
fn main() {
let mut pixel_paper = PixelPaper::<10, 10>::default();
let table = pixel_paper.get_mut_table();
}
您获得一个 10*10 的二维 PixelDescriptor
数组。
PixelDescriptor
描述了指定位置的像素。它是一个只有两个变体的简单枚举
pub enum PixelDescriptor {
Nothing, // Nothing there
Pixel(Pixel), // A pixel with some color.
}
现在您可以对像素做任何您想做的事情。
// ---- sniff ----
let red = PixelDescriptor::Pixel((255, 0, 0).into());
let green = [0, 255, 0];
let blue = (0, 0, 255);
let gray = 100; // (100, 100, 100)
table[0][0] = blue.into();
table[0][9] = red;
table[9][9] = green.into();
table[9][0] = gray.into();
// ---- sniff ----
您可以通过调用它的 into
来简单地将任何可以转换为 rgb 代码的东西转换为 PixelDescriptor
。
所有像素默认都是 PixelDescriptor::Nothing
。
然后最终将您的艺术作品保存到文件中。
// --- sniff ---
pixel_paper.save("arts/simple_1.png").unwrap()
}
运行代码并查看结果。
这就是整个想法!但还有更多...
use pixelar::{colors::*, positions::*, prelude::*};
fn main() {
let mut pixel_paper = PixelPaper::<6, 6>::default();
let rainbow = [
(250, 130, 0),
(240, 250, 0),
(60, 250, 0),
(0, 250, 220),
(0, 10, 250),
(210, 0, 250),
];
for i in 0..6 {
for j in 0..6 {
let choose = match j.cmp(&i) {
std::cmp::Ordering::Less => i,
std::cmp::Ordering::Equal => j,
std::cmp::Ordering::Greater => j,
};
pixel_paper.change_pixel_color((i, j), rainbow[choose])
}
}
pixel_paper.draw_straight_line(Red, RightTopEdge, LeftBottomEdge);
pixel_paper.save("arts/simple_2.png").unwrap()
}
这是结果
PixelBook
像素书是一个像素纸的集合,可以保存为GIF格式。
use pixelar::{colors::*, positions::*, prelude::*};
fn main() {
let mut paper = PixelPaper::<5, 5>::default();
let mut animation = PixelBook::new(Repeat::Infinite);
animation.add_paper(&paper);
for i in 0..5 {
paper.change_pixel_color((i, i), Red);
paper.change_pixel_color((i, 4 - i), Blue);
animation.add_paper(&paper);
}
animation.save("arts/animated_1.gif").unwrap();
}
Pixelar还在开发中,尚未完成!
依赖项
~21MB
~222K SLoC