z_mirror/PipeVisualizer

git clone https://git.raiguard.me/z_mirror/PipeVisualizer

Caleb HeuerCleanup32cb6c3

main
2.8 KiB121 linesraw
1--- Converts an RGB color value to HSV. Conversion formula
2--- adapted from http://en.wikipedia.org/wiki/HSV_color_space.
3--- @param input Color
4--- @return number, number, number, number
5local function rgb_to_hsv(input)
6  local r = input.r --[[@as number]]
7  local g = input.g --[[@as number]]
8  local b = input.b --[[@as number]]
9  local a = input.a --[[@as number]]
10  local max, min = math.max(r, g, b), math.min(r, g, b)
11  local h, s, v
12  v = max
13
14  local d = max - min
15  if max == 0 then
16    s = 0
17  else
18    s = d / max
19  end
20
21  if max == min then
22    h = 0 -- achromatic
23  else
24    if max == r then
25      h = (g - b) / d
26      if g < b then
27        h = h + 6
28      end
29    elseif max == g then
30      h = (b - r) / d + 2
31    elseif max == b then
32      h = (r - g) / d + 4
33    end
34    h = h / 6
35  end
36
37  return h, s, v, a
38end
39
40--- Converts an HSV color value to RGB. Conversion formula
41--- adapted from http://en.wikipedia.org/wiki/HSV_color_space.
42--- @param h number
43--- @param s number
44--- @param v number
45--- @param a number
46--- @return Color
47local function hsv_to_rgb(h, s, v, a)
48  local r, g, b
49
50  local i = math.floor(h * 6)
51  local f = h * 6 - i
52  local p = v * (1 - s)
53  local q = v * (1 - f * s)
54  local t = v * (1 - (1 - f) * s)
55
56  i = i % 6
57
58  if i == 0 then
59    r, g, b = v, t, p
60  elseif i == 1 then
61    r, g, b = q, v, p
62  elseif i == 2 then
63    r, g, b = p, v, t
64  elseif i == 3 then
65    r, g, b = p, q, v
66  elseif i == 4 then
67    r, g, b = t, p, v
68  elseif i == 5 then
69    r, g, b = v, p, q
70  end
71
72  return { r = r, g = g, b = b, a = a }
73end
74
75--- @param e EventData.CustomInputEvent
76local function on_color_by_system_changed(e)
77  global.color_by_system[e.player_index] = not global.color_by_system[e.player_index]
78  local player = game.get_player(e.player_index) --[[@as LuaPlayer]]
79  player.create_local_flying_text({
80    text = { global.color_by_system[e.player_index] and "message.pv-color-by-system" or "message.pv-color-by-fluid" },
81    create_at_cursor = true,
82  })
83end
84
85local function generate_system_colors()
86  --- @type Color[]
87  global.system_colors = {}
88  for s = 0.8, 0.3, -0.1 do
89    for h = 0, 360, 30 do
90      global.system_colors[#global.system_colors + 1] = hsv_to_rgb(h / 360, s, 1, 1)
91    end
92  end
93end
94
95local function generate_fluid_colors()
96  --- @type table<string, Color>
97  local colors = {}
98  for name, fluid in pairs(game.fluid_prototypes) do
99    local h, s, v, a = rgb_to_hsv(fluid.base_color)
100    v = 1
101    colors[name] = hsv_to_rgb(h, s, v, a)
102  end
103  global.fluid_colors = colors
104end
105
106local function initialize()
107  global.color_by_system = {}
108  generate_fluid_colors()
109  generate_system_colors()
110end
111
112local colors = {}
113
114colors.on_init = initialize
115colors.on_configuration_changed = initialize
116
117colors.events = {
118  ["pv-color-by-fluid-system"] = on_color_by_system_changed,
119}
120
121return colors