Skip to content

Commit c74a9d4

Browse files
committed
Camera Flash: Various Optimizations
1 parent d859260 commit c74a9d4

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

custom_events/Camera Flash.lua

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,56 +59,63 @@ local function rgbToHex(r, g, b)
5959
g = math.max(0, math.min(255, g))
6060
b = math.max(0, math.min(255, b))
6161

62-
local hexR = string.format("%02X", r)
63-
local hexG = string.format("%02X", g)
64-
local hexB = string.format("%02X", b)
65-
return hexR .. hexG .. hexB
62+
return string.format("%02X%02X%02X", r, g, b)
6663
end
6764

68-
-- Parses the camera and forced value from the input string.
65+
-- Validates hex colour format
66+
local function isValidHex(hex)
67+
return #hex == 6 and not hex:find("[^0-9A-Fa-f]")
68+
end
69+
70+
-- Parses the camera and forced value from the input string
6971
local function parseCameraValue(value)
7072
if not value or value == "" then
7173
return defaultCamera, defaultForced
7274
end
7375

74-
local camera, isForced = string.match(value:gsub(" ", ""), "(.*),(%a+)")
76+
local cleanValue = value:gsub(" ", "")
77+
local camera, forcedStr = string.match(cleanValue, "([^,]+),(%a+)")
7578

76-
if validCameras[camera] and isForced == "true" then
77-
return camera, true
78-
elseif validCameras[camera] then
79-
return camera, false
79+
if camera and validCameras[camera] then
80+
return camera, forcedStr == "true"
8081
end
8182

8283
return defaultCamera, defaultForced
8384
end
8485

85-
-- Parses the colour and duration from the input string.
86+
-- Parses the colour and duration from the input string
8687
local function parseFlashValue(value)
8788
if not value or value == "" then
8889
return defaultColour, defaultDuration
8990
end
9091

91-
local colour, duration = string.match(value:gsub(" ", ""), "(.*),(.*)")
92+
local cleanValue = value:gsub(" ", "")
93+
local parts = {}
94+
for part in string.gmatch(cleanValue, "[^,]+") do
95+
table.insert(parts, part)
96+
end
9297

93-
if not colour or not duration then
98+
if #parts < 2 then
9499
return defaultColour, defaultDuration
95100
end
96101

97-
if colour:lower() == "random" then
98-
local r = math.random(0, 255)
99-
local g = math.random(0, 255)
100-
local b = math.random(0, 255)
101-
colour = rgbToHex(r, g, b)
102-
return colour, tonumber(duration) or defaultDuration
103-
else
104-
local r, g, b = string.match(colour:gsub(" ", ""), "(%d+),(%d+),(%d+)")
102+
local duration = tonumber(parts[#parts]) or defaultDuration
103+
104+
-- Handle 'random' colour
105+
if parts[1]:lower() == "random" then
106+
return rgbToHex(math.random(0, 255), math.random(0, 255), math.random(0, 255)), duration
107+
end
108+
109+
-- Handle RGB (3 parts + duration)
110+
if #parts == 4 then
111+
local r, g, b = tonumber(parts[1]), tonumber(parts[2]), tonumber(parts[3])
105112
if r and g and b then
106-
colour = rgbToHex(tonumber(r), tonumber(g), tonumber(b))
107-
return colour, tonumber(duration) or defaultDuration
108-
else
109-
return colour, tonumber(duration) or defaultDuration
113+
return rgbToHex(r, g, b), duration
110114
end
111115
end
116+
117+
-- Handle HEX (1 part + duration)
118+
return parts[1], duration
112119
end
113120

114121
-- #####################################################################
@@ -117,6 +124,7 @@ end
117124

118125
function onEvent(name, value1, value2)
119126
if (name == "Camera Flash" or name == "Camera_Flash") and flashingLights then
127+
120128
local camera, isForced = parseCameraValue(value1)
121129
local colour, duration = parseFlashValue(value2)
122130

@@ -126,15 +134,10 @@ function onEvent(name, value1, value2)
126134
debugPrint(" Camera: " .. camera .. " | Forced: " .. tostring(isForced))
127135
debugPrint(" Colour: " .. colour .. " | Duration: " .. duration)
128136

129-
-- Validate hex colour format
130-
local charIndex = string.find(colour, "[^0-9A-Fa-f]")
131-
if colour:len() == 6 and charIndex then
132-
debugPrint(" WARNING: Hex colour contains invalid character at index " .. charIndex)
133-
elseif colour:len() ~= 6 then
134-
debugPrint(" WARNING: Hex colour length is " .. colour:len() .. ", expected 6")
137+
if not isValidHex(colour) then
138+
debugPrint(" WARNING: Invalid hex colour format (expected 6 hex characters)")
135139
end
136140
end
137-
138141
cameraFlash(camera, colour, duration, isForced)
139142
end
140143
end

0 commit comments

Comments
 (0)