Repository navigation
Expand file tree
/
Copy pathrandom-colors.lua
More file actions
70 lines (59 loc) · 2.4 KB
/
Copy pathrandom-colors.lua
File metadata and controls
70 lines (59 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
-- Title: Random Colors
-- Path: Colors
-- Description: Creates high-contrast random foreground colors while preserving backgrounds. Avoids low-contrast combinations.
-- Author: Eduardo Castillo (hellocodelinux@gmail.com)
-- A function to determine if a color is considered "bright."
-- Bright colors are those with a value greater than or equal to 7.
local function is_bright_color(color)
return color >= 7
end
-- A function to generate a random dark color.
-- Dark colors are in the range 1 to 6.
local function get_random_dark()
local color = math.random(1, 6)
return color
end
-- A function to generate a random bright color.
-- Bright colors are in the range 9 to 14.
local function get_random_bright()
return math.random(9, 14)
end
-- Ensure that start_x is less than or equal to end_x. If not, swap them.
if start_x > end_x then
start_x, end_x = end_x, start_x
end
-- Ensure that start_y is less than or equal to end_y. If not, swap them.
if start_y > end_y then
start_y, end_y = end_y, start_y
end
-- Seed the random number generator with the current time to ensure different results each run.
math.randomseed(os.time())
-- Loop through each row (y-coordinate) from start_y to end_y.
for y = start_y, end_y do
-- Loop through each column (x-coordinate) from start_x to end_x.
for x = start_x, end_x do
-- Retrieve the background color at the current position (x, y).
local bg = buf:get_bg(x, y)
-- Initialize the foreground color variable.
local color_fg
-- Determine whether the background color is bright or dark.
-- If the background is bright, choose a dark foreground color.
-- If the background is dark, choose a bright foreground color.
if is_bright_color(bg) then
color_fg = get_random_dark()
else
color_fg = get_random_bright()
end
-- Ensure that the foreground color is not the same as the background color.
-- This avoids low-contrast combinations where the text would blend into the background.
while color_fg == bg do
if is_bright_color(bg) then
color_fg = get_random_dark()
else
color_fg = get_random_bright()
end
end
-- Set the foreground color at the current position (x, y) to the chosen color.
buf:set_fg(x, y, color_fg)
end
end