Градиент вместо фигни какой то

This commit is contained in:
2025-12-18 22:24:30 +03:00
parent 4f0fb09185
commit 183726aed4

View File

@@ -26,37 +26,28 @@ pub fn deinit(self: *Canvas) void {
} }
} }
/// Заполнить canvas случайным цветом на CPU /// Заполнить canvas градиентом
pub fn fillRandomColor(self: *Canvas) !void { pub fn fillRandomColor(self: *Canvas) !void {
var prng = std.Random.DefaultPrng.init(@intCast(std.time.microTimestamp()));
const random = prng.random();
// Выделить буфер пиксельных данных // Выделить буфер пиксельных данных
const pixels = try self.allocator.alloc(Color.PMA, @as(usize, self.width) * self.height); const pixels = try self.allocator.alloc(Color.PMA, @as(usize, self.width) * self.height);
defer self.allocator.free(pixels); defer self.allocator.free(pixels);
// Заполнить случайными цветами // Сгенерировать случайные цвета градиента
const r = random.int(u8); var prng = std.Random.DefaultPrng.init(@intCast(std.time.microTimestamp()));
const g = random.int(u8); const random = prng.random();
const b = random.int(u8); const start_color = Color.PMA{ .r = random.int(u8), .g = random.int(u8), .b = random.int(u8), .a = 255 };
const end_color = Color.PMA{ .r = random.int(u8), .g = random.int(u8), .b = random.int(u8), .a = 255 };
var prev: dvui.Color.PMA = .{ .r = r, .g = g, .b = b, .a = 255 }; var y: u32 = 0;
for (pixels) |*pixel| { while (y < self.height) : (y += 1) {
const r_delta = random.intRangeAtMost(i16, -1, 1); var x: u32 = 0;
const g_delta = random.intRangeAtMost(i16, -1, 1); while (x < self.width) : (x += 1) {
const b_delta = random.intRangeAtMost(i16, -1, 1); const factor = @as(f32, @floatFromInt(x)) / @as(f32, @floatFromInt(self.width - 1));
const r = @as(u8, @intFromFloat(@as(f32, @floatFromInt(start_color.r)) + factor * (@as(f32, @floatFromInt(end_color.r)) - @as(f32, @floatFromInt(start_color.r)))));
const r_new: i16 = @as(i16, prev.r) + r_delta; const g = @as(u8, @intFromFloat(@as(f32, @floatFromInt(start_color.g)) + factor * (@as(f32, @floatFromInt(end_color.g)) - @as(f32, @floatFromInt(start_color.g)))));
const g_new: i16 = @as(i16, prev.g) + g_delta; const b = @as(u8, @intFromFloat(@as(f32, @floatFromInt(start_color.b)) + factor * (@as(f32, @floatFromInt(end_color.b)) - @as(f32, @floatFromInt(start_color.b)))));
const b_new: i16 = @as(i16, prev.b) + b_delta; pixels[y * self.width + x] = .{ .r = r, .g = g, .b = b, .a = 255 };
}
pixel.* = .{
.r = @intCast(std.math.clamp(r_new, 0, 255)),
.g = @intCast(std.math.clamp(g_new, 0, 255)),
.b = @intCast(std.math.clamp(b_new, 0, 255)),
.a = 255,
};
prev = pixel.*;
} }
// Удалить старую текстуру // Удалить старую текстуру