Faster rgb to hex

This commit is contained in:
NO11 2021-07-12 18:05:52 +00:00
parent 509568b4b0
commit 5ceb48fcb1
1 changed files with 20 additions and 41 deletions

View File

@ -5,30 +5,9 @@ minetest.register_on_leaveplayer(function(player)
end) end)
-- Totem particle registration -- Totem particle registration
function rgb_to_hex(rgb)
local hexadecimal = "#"
for key, value in pairs(rgb) do function rgb_to_hex(r, g, b)
local hex = "" return string.format("%02x%02x%02x", r, g, b)
while value > 0 do
local index = math.fmod(value, 16) + 1
value = math.floor(value / 16)
hex = string.sub("0123456789ABCDEF", index, index) .. hex
end
local len = string.len(hex)
if len == 0 then
hex = "00"
elseif len == 1 then
hex = "0" .. hex
end
hexadecimal = hexadecimal .. hex
end
return hexadecimal
end end
minetest.register_entity("mcl_totems:totem_particle", { minetest.register_entity("mcl_totems:totem_particle", {
@ -45,12 +24,12 @@ minetest.register_entity("mcl_totems:totem_particle", {
on_activate = function(self, staticdata) on_activate = function(self, staticdata)
local color local color
if math.random(0, 3) == 0 then if math.random(0, 3) == 0 then
color = rgb_to_hex({ (0.6 + math.random() * 0.2) * 255, (0.6 + math.random() * 0.3) * 255, (math.random() * 0.2) * 255 }) color = rgb_to_hex( 153 + math.random() * 51, 153 + math.random() * 76.5, math.random() * 51)
else else
color = rgb_to_hex({ (0.1 + math.random() * 0.4) * 255, (0.6 + math.random() * 0.3) * 255, (math.random() * 0.2) * 255 }) color = rgb_to_hex(25.5 + math.random() * 102, 153 + math.random() * 76.5, math.random() * 51)
end end
self.object:set_properties({ self.object:set_properties({
textures = { "mcl_particles_totem"..math.random(1, 4)..".png^[colorize:"..color } textures = { "mcl_particles_totem"..math.random(1, 4)..".png^[colorize:#"..color }
}) })
local t = math.random(1, 2)*math.random() local t = math.random(1, 2)*math.random()
minetest.after(t, function() minetest.after(t, function()