Поправлены цвета
This commit is contained in:
@@ -17,7 +17,7 @@ const default_common_data = [_]PropertyData{
|
||||
.{ .visible = true },
|
||||
.{ .opacity = 1.0 },
|
||||
.{ .locked = false },
|
||||
.{ .stroke_rgba = 0x000000FF },
|
||||
.{ .stroke_rgba = 0x000000FF }, // 0xRRGGBBAA: чёрный, полная непрозрачность
|
||||
.{ .thickness = 2.0 },
|
||||
};
|
||||
|
||||
|
||||
@@ -19,7 +19,9 @@ pub const Data = union(enum) {
|
||||
|
||||
points: std.ArrayList(Point2_f),
|
||||
|
||||
/// Цвет заливки: u32 в формате 0xRRGGBBAA (R старший байт, A младший).
|
||||
fill_rgba: u32,
|
||||
/// Цвет обводки: u32 в формате 0xRRGGBBAA (R старший байт, A младший).
|
||||
stroke_rgba: u32,
|
||||
|
||||
thickness: f32,
|
||||
|
||||
@@ -1,130 +0,0 @@
|
||||
const std = @import("std");
|
||||
const Document = @import("Document.zig");
|
||||
const Object = Document.Object;
|
||||
const shape = @import("shape/shape.zig");
|
||||
const basic_models = @import("basic_models.zig");
|
||||
const Size_f = basic_models.Size_f;
|
||||
const Point2_f = basic_models.Point2_f;
|
||||
const Scale2_f = basic_models.Scale2_f;
|
||||
const Radii_f = basic_models.Radii_f;
|
||||
|
||||
fn randFloat(rng: std.Random, min: f32, max: f32) f32 {
|
||||
return min + (max - min) * rng.float(f32);
|
||||
}
|
||||
|
||||
fn randRgba(rng: std.Random) u32 {
|
||||
const r = rng.int(u8);
|
||||
const g = rng.int(u8);
|
||||
const b = rng.int(u8);
|
||||
const a: u8 = @intCast(rng.intRangeLessThan(usize, 128, 256));
|
||||
return r | (@as(u32, g) << 8) | (@as(u32, b) << 16) | (@as(u32, a) << 24);
|
||||
}
|
||||
|
||||
fn randomShapeKind(rng: std.Random) Object.ShapeKind {
|
||||
const shapes_implemented = [_]Object.ShapeKind{ .line, .ellipse, .broken };
|
||||
return shapes_implemented[rng.intRangeLessThan(usize, 0, shapes_implemented.len)];
|
||||
}
|
||||
|
||||
/// Случайно заполняет все доступные свойства объекта; позиция — в пределах документа.
|
||||
fn randomizeObjectProperties(allocator: std.mem.Allocator, doc_size: *const Size_f, obj: *Object, rng: std.Random) !void {
|
||||
const margin: f32 = 8;
|
||||
const max_x = @max(0, doc_size.w - margin);
|
||||
const max_y = @max(0, doc_size.h - margin);
|
||||
|
||||
try obj.setProperty(allocator, .{ .data = .{
|
||||
.position = .{
|
||||
.x = randFloat(rng, margin, if (max_x > margin) max_x else margin),
|
||||
.y = randFloat(rng, margin, if (max_y > margin) max_y else margin),
|
||||
},
|
||||
} });
|
||||
try obj.setProperty(allocator, .{ .data = .{ .angle = randFloat(rng, 0, 2 * std.math.pi) } });
|
||||
try obj.setProperty(allocator, .{ .data = .{
|
||||
.scale = .{
|
||||
.scale_x = randFloat(rng, 0.25, 2.0),
|
||||
.scale_y = randFloat(rng, 0.25, 2.0),
|
||||
},
|
||||
} });
|
||||
try obj.setProperty(allocator, .{ .data = .{ .visible = true } });
|
||||
try obj.setProperty(allocator, .{ .data = .{ .opacity = randFloat(rng, 0.3, 1.0) } });
|
||||
try obj.setProperty(allocator, .{ .data = .{ .locked = rng.boolean() } });
|
||||
|
||||
const stroke = randRgba(rng);
|
||||
try obj.setProperty(allocator, .{ .data = .{ .stroke_rgba = stroke } });
|
||||
obj.setProperty(allocator, .{ .data = .{ .fill_rgba = randRgba(rng) } }) catch {};
|
||||
const thickness = randFloat(rng, max_x * 0.01, max_x * 0.1);
|
||||
try obj.setProperty(allocator, .{ .data = .{ .thickness = thickness } });
|
||||
|
||||
switch (obj.shape) {
|
||||
.line => {
|
||||
const len = randFloat(rng, 20, @min(doc_size.w, doc_size.h) * 0.5);
|
||||
const angle = randFloat(rng, 0, 2 * std.math.pi);
|
||||
try obj.setProperty(allocator, .{ .data = .{
|
||||
.end_point = .{
|
||||
.x = std.math.cos(angle) * len,
|
||||
.y = std.math.sin(angle) * len,
|
||||
},
|
||||
} });
|
||||
},
|
||||
.ellipse => {
|
||||
const max_r = @min(120, @min(doc_size.w / 4, doc_size.h / 4));
|
||||
try obj.setProperty(allocator, .{ .data = .{
|
||||
.radii = .{
|
||||
.x = randFloat(rng, 8, @max(8, max_r)),
|
||||
.y = randFloat(rng, 8, @max(8, max_r)),
|
||||
},
|
||||
} });
|
||||
},
|
||||
.broken => {
|
||||
var points = std.ArrayList(Point2_f).empty;
|
||||
const n = rng.intRangeLessThan(usize, 2, 9);
|
||||
var x: f32 = 0;
|
||||
var y: f32 = 0;
|
||||
for (0..n) |_| {
|
||||
try points.append(allocator, .{ .x = x, .y = y });
|
||||
x += randFloat(rng, -40, 80);
|
||||
y += randFloat(rng, -30, 60);
|
||||
}
|
||||
try obj.setProperty(allocator, .{ .data = .{ .points = points } });
|
||||
},
|
||||
.arc => {},
|
||||
}
|
||||
}
|
||||
|
||||
/// Создаёт в документе случайное количество фигур (в т.ч. вложенных).
|
||||
/// У каждой фигуры все доступные свойства задаются случайно; позиция — в пределах документа.
|
||||
/// Реализованные типы: line, ellipse, broken.
|
||||
pub fn addRandomShapes(doc: *Document, rng: std.Random) !void {
|
||||
const max_total: usize = 80;
|
||||
var total_count: usize = 0;
|
||||
const allocator = doc.allocator;
|
||||
|
||||
const n_root = rng.intRangeLessThan(usize, 6, 15);
|
||||
for (0..n_root) |_| {
|
||||
if (total_count >= max_total) break;
|
||||
var obj = try shape.createObject(allocator, randomShapeKind(rng));
|
||||
try randomizeObjectProperties(allocator, &doc.size, &obj, rng);
|
||||
try doc.addObject(obj);
|
||||
total_count += 1;
|
||||
}
|
||||
|
||||
var stack = std.ArrayList(*Object).empty;
|
||||
defer stack.deinit(allocator);
|
||||
for (doc.objects.items) |*obj| {
|
||||
try stack.append(allocator, obj);
|
||||
}
|
||||
while (stack.pop()) |obj| {
|
||||
if (total_count >= max_total) continue;
|
||||
const n_children = rng.intRangeLessThan(usize, 0, 2);
|
||||
const base_len = obj.children.items.len;
|
||||
for (0..n_children) |_| {
|
||||
if (total_count >= max_total) break;
|
||||
var child = try shape.createObject(allocator, randomShapeKind(rng));
|
||||
try randomizeObjectProperties(allocator, &doc.size, &child, rng);
|
||||
try obj.addChild(allocator, child);
|
||||
total_count += 1;
|
||||
}
|
||||
for (obj.children.items[base_len..]) |*child| {
|
||||
try stack.append(allocator, child);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user