Skip to content

Commit

Permalink
initiated migration to the new and improved API (check README)
Browse files Browse the repository at this point in the history
  • Loading branch information
nitrogenez committed Sep 21, 2023
1 parent c9cbade commit d9ddf3e
Show file tree
Hide file tree
Showing 14 changed files with 457 additions and 353 deletions.
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

Prism is a utility library for managing colors and colorspaces written in Zig.

> **NOTE**
> Prism is still under active development.
> Please, raise an issue, if you encounter any bugs.
> **WARNING**
> There are ongoing major API changes. Please, be cautious
> and follow the development process. All changes are
> documented in the code directly. Old API is already
> set to be deprecated.
# Why Prism?
Prism is lightweight, fast, and easy to use in general. There is no boilerplate, just import the library, and start using colors and convert them into any of supported colorspaces, including CIE l\*a\*b\*, which is a highly useful colorspace for working with colors that our eyes actually percept.
Expand Down Expand Up @@ -60,22 +62,24 @@ If you are willing to make Prism better (or worse), you may follow the instructi
9. You're GTG, enjoy your profile pic in a contributors list :)

# Colorspace Support
| NAME | STATE |
| ---- | ----------- |
| CMYK | **FULL** |
| HSI | **PARTIAL** |
| HSL | **FULL** |
| LAB | **FULL** |
| YIQ | **PARITAL** |
| HSV | **FULL** |
| RGB | **FULL** |
| XYZ | **FULL** |
| NAME | STATE | NEW |
| ---- | ----------- | ------------ |
| CMYK | **FULL** | **NO** |
| HSI | **PARTIAL** | **IN TODOS** |
| HSL | **FULL** | **FULL** |
| LAB | **FULL** | **IN TODOS** |
| YIQ | **PARITAL** | **NO** |
| HSV | **FULL** | **PARTIAL** |
| RGB | **FULL** | **FULL** |
| XYZ | **FULL** | **FULL** |

### Meaning
+ **NAME** - Name of the colorspace
+ **STATE** - A colorspace support state
+ **FULL** - A full-featured colorspace support
+ **PARTIAL** - It kinda works, but is lacking functionality
+ **NO** - No support at all
+ **IN TODOS** - Planned and yet to be implemented

# License
Prism is licensed under a **BSD-3-Clause "New" or "Revised" License**. See [LICENSE](LICENSE) to learn more.
Expand Down
61 changes: 24 additions & 37 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,56 +1,43 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const lib_version = std.SemanticVersion.parse("0.1.4") catch unreachable;
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{});

const lib = b.addStaticLibrary(.{
.name = "prism",
.root_source_file = .{ .path = "src/prism.zig" },
.target = target,
.optimize = optimize,
.version = lib_version,
_ = b.addModule("prism", .{
.source_file = .{ .path = "src/prism.zig" },
});

const lib_shared = b.addSharedLibrary(.{
const lib = b.addStaticLibrary(.{
.name = "prism",
.root_source_file = .{ .path = "src/prism.zig" },
.target = target,
.optimize = optimize,
.version = lib_version,
});

const prism_mod = b.createModule(.{ .source_file = .{ .path = "src/prism.zig" } });

const shades = b.addExecutable(.{
.name = "shades",
.root_source_file = .{ .path = "src/shades.zig" },
.target = target,
.optimize = optimize,
});

const rainbow = b.addExecutable(.{
.name = "rainbow",
.root_source_file = .{ .path = "src/rainbow.zig" },
.target = target,
.optimize = optimize,
});
rainbow.addModule("prism", prism_mod);
shades.addModule("prism", prism_mod);

b.installArtifact(lib);
b.installArtifact(lib_shared);
b.installArtifact(rainbow);
b.installArtifact(shades);

const test_step = b.step("test", "Run tests");
const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/tests.zig" },
.name = "prism-tests",
.root_source_file = .{ .path = "src/prism.zig" },
.target = target,
.optimize = optimize,
});

const run_main_tests = b.addRunArtifact(main_tests);
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&run_main_tests.step);
main_tests.linkLibrary(lib);
test_step.dependOn(&b.addRunArtifact(main_tests).step);

const examples = [_]*std.Build.Step.Compile{
b.addExecutable(.{
.name = "madness",
.root_source_file = .{ .path = "src/examples/madness.zig" },
.target = target,
.optimize = optimize,
}),
};

for (examples) |e| {
e.addModule("prism", b.modules.get("prism").?);
e.linkLibrary(lib);
b.installArtifact(e);
}
}
4 changes: 4 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.{
.name = "prism",
.version = "0.2.0",
}
85 changes: 27 additions & 58 deletions src/colorspaces/hsi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,54 +7,34 @@ pub const HSI = struct {
s: f32 = 0.0,
i: f32 = 0.0,

// Broken.
pub fn toRGB(self: *const HSI) RGB {
var o = RGB{};
var rgb: [3]f32 = .{ 0.0, 0.0, 0.0 };

const i = self.i;
const s = self.s;
const h = self.h;
const s = self.s;
const i = self.i;
const is = i * s;

if (h < 0.000001) {
o = .{
.r = i + (is * is),
.g = i - is,
.b = i - is,
};
rgb = .{ i + is * is, i - is, i - is };
} else if (0.0 < h and h < 120.0) {
o = .{
.r = i + is * @cos(h) / @cos(60 - h),
.g = i + is * (1.0 - @cos(h) / @cos(60 - h)),
.b = i - is,
};
rgb[0] = i + is * @cos(h) / @cos(60.0 - h);
rgb[1] = i + is * (1.0 - @cos(h) / @cos(60.0 - h));
rgb[2] = i - is;
} else if (h >= 120.000005 and h <= 120.5) {
o = .{
.r = i - is,
.g = i + (is * is),
.b = i - is,
};
rgb = .{ i - is, i + is * is, i - is };
} else if (120.0 < h and h < 240.0) {
o = .{
.r = i - is,
.g = i + is * @cos(h - 120.0) / @cos(180.0 - h),
.b = i + is * (1.0 - @cos(h - 120.0) / @cos(180.0 - h)),
};
rgb[0] = i - is;
rgb[1] = i + is * @cos(h - 120.0) / @cos(180.0 - h);
rgb[2] = i + is * (1.0 - @cos(h - 120.0) / @cos(180.0 - h));
} else if (h >= 240.000005 and h <= 240.5) {
o = .{ .r = i - is, .g = i - is, .b = i + (is * is) };
rgb = .{ i - is, i - is, i + is * is };
} else {
o = .{
.r = i + is * (1.0 - @cos(h - 240) / @cos(300.0 - h)),
.g = i - is,
.b = i + is * @cos(h - 240.0) / @cos(300.0 - h),
};
rgb[0] = i + is * (1.0 - @cos(h - 240.0) / @cos(300.0 - h));
rgb[1] = i - is;
rgb[2] = i + is * (@cos(h - 240.0) / @cos(300.0 - h));
}

o.r = math.clamp(o.r, 0.0, 255.0);
o.g = math.clamp(o.g, 0.0, 255.0);
o.b = math.clamp(o.b, 0.0, 255.0);

return o;
return rgb;
}

pub fn fromRGB(from: *const RGB) HSI {
Expand All @@ -64,36 +44,25 @@ pub const HSI = struct {
const min = @min(n.r, n.g, n.b);
const d = max - min;

var o = HSI{};
var hsi: [3]f32 = .{ 0.0, 0.0, 0.0 };

if (d < 0.00001) {
o.s = 0;
o.h = 0;
return o;
}
if (d < 0.00001 or max < 0.00001) return hsi;

if (max > 0.0) {
o.s = (d / max);
} else {
o.s = 0.0;
o.h = 0.0;
return o;
}
hsi[1] = (d / max);

if (n.r >= max) {
o.h = (n.g - n.b) / d;
hsi[0] = (n.g - n.b) / d;
} else if (n.g >= max) {
o.h = 2.0 + (n.b - n.r) / d;
hsi[0] = 2.0 + (n.b - n.r) / d;
} else {
o.h = 4.0 + (n.r - n.g) / d;
hsi[0] = 4.0 + (n.r - n.g) / d;
}

o.h *= 60.0;
o.i = (n.r + n.g + n.b) / 3;
hsi[0] *= 60.0;
hsi[2] = (n.r + n.g + n.b) / 3;

if (o.h < 0.0) {
o.h = 360.0;
}
return o;
if (hsi[0] < 0.0) hsi[0] = 360.0;

return hsi;
}
};
2 changes: 1 addition & 1 deletion src/colorspaces/xyz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub const XYZ = struct {
}

for (mat, 0..) |k, i| {
xyz[i] = (rgb[0] * k[0]) + (rgb[1] * k[1]) * (rgb[2] * k[2]);
xyz[i] = rgb[0] * k[0] + rgb[1] * k[1] * (rgb[2] * k[2]);
}
return .{ .x = xyz[0], .y = xyz[1], .z = xyz[2] };
}
Expand Down
40 changes: 40 additions & 0 deletions src/examples/madness.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const std = @import("std");
const prism = @import("prism");

pub fn main() !void {
var alloc = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer alloc.deinit();

const args = try std.process.argsAlloc(alloc.allocator());
defer std.process.argsFree(alloc.allocator(), args);

var stdout = std.io.getStdOut().writer();
var madness_text: []const u8 = "MADNESS!";

if (args.len > 1) {
for (args[1..]) |arg| {
madness_text = arg;
}
}

var rng = std.rand.DefaultPrng.init(@as(
u64,
@truncate(@as(u128, @bitCast(std.time.nanoTimestamp()))),
));

while (true) {
const rgb = prism.RGB{
.r = rng.random().float(f64),
.g = rng.random().float(f64),
.b = rng.random().float(f64),
};
const c = rgb.as8bitArray();

std.time.sleep(90 * std.time.ns_per_ms);
try stdout.print(
"\r\x1b[38;2;{d:.0};{d:.0};{d:.0}m{s}\x1b[0m",
.{ c[0], c[1], c[2], madness_text },
);
}
_ = try stdout.write("\x1b[0m\n");
}
35 changes: 19 additions & 16 deletions src/prism.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const std = @import("std");

pub const RGB = @import("spaces/RGB.zig");
pub const HSI = @import("spaces/HSI.zig");
pub const HSV = @import("spaces/HSV.zig");
pub const XYZ = @import("spaces/XYZ.zig");

pub const spaces = struct {
pub const RGB = @import("colorspaces/rgb.zig").RGB;
pub const HSL = @import("colorspaces/hsl.zig").HSL;
Expand All @@ -12,21 +17,19 @@ pub const spaces = struct {
};

pub const colors = struct {
const RGB = spaces.RGB;

pub const Red: RGB = .{ .r = 255 };
pub const Green: RGB = .{ .g = 255 };
pub const Blue: RGB = .{ .b = 255 };
pub const Black: RGB = .{};
pub const White: RGB = .{ .r = 255, .g = 255, .b = 255 };
pub const Red: spaces.RGB = .{ .r = 255 };
pub const Green: spaces.RGB = .{ .g = 255 };
pub const Blue: spaces.RGB = .{ .b = 255 };
pub const Black: spaces.RGB = .{};
pub const White: spaces.RGB = .{ .r = 255, .g = 255, .b = 255 };

pub const CalmingCoral: RGB = .{ .r = 233, .g = 150, .b = 122 };
pub const VelvetViolet: RGB = .{ .r = 128, .b = 128 };
pub const PacificPink: RGB = .{ .r = 219, .g = 112, .b = 147 };
pub const Pink: RGB = .{ .r = 255, .g = 192, .b = 203 };
pub const MistyRose1: RGB = .{ .r = 255, .g = 228, .b = 225 };
pub const Linen: RGB = .{ .r = 250, .g = 240, .b = 230 };
pub const SteelBlue: RGB = .{ .r = 70, .g = 130, .b = 180 };
pub const StrongAzure: RGB = .{ .g = 87, .b = 184 };
pub const Gold1: RGB = .{ .r = 255, .g = 215 };
pub const CalmingCoral: spaces.RGB = .{ .r = 233, .g = 150, .b = 122 };
pub const VelvetViolet: spaces.RGB = .{ .r = 128, .b = 128 };
pub const PacificPink: spaces.RGB = .{ .r = 219, .g = 112, .b = 147 };
pub const Pink: spaces.RGB = .{ .r = 255, .g = 192, .b = 203 };
pub const MistyRose1: spaces.RGB = .{ .r = 255, .g = 228, .b = 225 };
pub const Linen: spaces.RGB = .{ .r = 250, .g = 240, .b = 230 };
pub const SteelBlue: spaces.RGB = .{ .r = 70, .g = 130, .b = 180 };
pub const StrongAzure: spaces.RGB = .{ .g = 87, .b = 184 };
pub const Gold1: spaces.RGB = .{ .r = 255, .g = 215 };
};
Loading

0 comments on commit d9ddf3e

Please sign in to comment.