2021-02-06 09:29:49 -05:00
|
|
|
//
|
|
|
|
// Enums are really just a set of numbers. You can leave the
|
|
|
|
// numbering up to the compiler, or you can assign them
|
|
|
|
// explicitly. You can even specify the numeric type used.
|
|
|
|
//
|
|
|
|
// const Stuff = enum(u8){ foo = 16 };
|
|
|
|
//
|
2021-04-10 14:45:25 -04:00
|
|
|
// You can get the integer out with a builtin function,
|
|
|
|
// @enumToInt(). We'll learn about builtins properly in a later
|
|
|
|
// exercise.
|
2021-02-06 09:29:49 -05:00
|
|
|
//
|
2023-06-22 05:41:41 -04:00
|
|
|
// const my_stuff: u8 = @enumToInt(Stuff.foo);
|
2021-02-06 09:29:49 -05:00
|
|
|
//
|
|
|
|
// Note how that built-in function starts with "@" just like the
|
|
|
|
// @import() function we've been using.
|
|
|
|
//
|
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
// Zig lets us write integers in hexadecimal format:
|
|
|
|
//
|
2021-02-15 16:55:44 -05:00
|
|
|
// 0xf (is the value 15 in hex)
|
2021-02-06 09:29:49 -05:00
|
|
|
//
|
|
|
|
// Web browsers let us specify colors using a hexadecimal
|
|
|
|
// number where each byte represents the brightness of the
|
|
|
|
// Red, Green, or Blue component (RGB) where two hex digits
|
|
|
|
// are one byte with a value range of 0-255:
|
|
|
|
//
|
|
|
|
// #RRGGBB
|
|
|
|
//
|
|
|
|
// Please define and use a pure blue value Color:
|
2021-02-15 16:55:44 -05:00
|
|
|
const Color = enum(u32) {
|
|
|
|
red = 0xff0000,
|
2021-02-06 09:29:49 -05:00
|
|
|
green = 0x00ff00,
|
2021-02-15 16:55:44 -05:00
|
|
|
blue = ???,
|
2021-02-06 09:29:49 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
pub fn main() void {
|
2021-02-26 00:19:35 -05:00
|
|
|
// Remember Zig's multi-line strings? Here they are again.
|
2021-02-06 09:29:49 -05:00
|
|
|
// Also, check out this cool format string:
|
|
|
|
//
|
|
|
|
// {x:0>6}
|
|
|
|
// ^
|
|
|
|
// x type ('x' is lower-case hexadecimal)
|
|
|
|
// : separator (needed for format syntax)
|
|
|
|
// 0 padding character (default is ' ')
|
|
|
|
// > alignment ('>' aligns right)
|
|
|
|
// 6 width (use padding to force width)
|
|
|
|
//
|
|
|
|
// Please add this formatting to the blue value.
|
|
|
|
// (Even better, experiment without it, or try parts of it
|
|
|
|
// to see what prints!)
|
|
|
|
std.debug.print(
|
|
|
|
\\<p>
|
|
|
|
\\ <span style="color: #{x:0>6}">Red</span>
|
|
|
|
\\ <span style="color: #{x:0>6}">Green</span>
|
|
|
|
\\ <span style="color: #{}">Blue</span>
|
|
|
|
\\</p>
|
2021-04-04 16:23:27 -04:00
|
|
|
\\
|
2021-02-15 16:55:44 -05:00
|
|
|
, .{
|
2023-06-22 04:23:01 -04:00
|
|
|
@intFromEnum(Color.red),
|
|
|
|
@intFromEnum(Color.green),
|
|
|
|
@intFromEnum(???), // Oops! We're missing something!
|
2021-02-15 16:55:44 -05:00
|
|
|
});
|
2021-02-06 09:29:49 -05:00
|
|
|
}
|