I do know that editable images are still in beta but this is when they come out of beta and therefore people can use this module.
Hello people of the internet, I am back with another terrible resource!
Anyways here is the module:
local AvgColorFinder = {}
function AvgColorFinder.GetAverageColor(Image:string,Size:Vector2)
local EI = game:GetService("AssetService"):CreateEditableImageAsync(Image) EI.Size = Size EI = EI:ReadPixels(Vector2.zero,Size)
local r,g,b = 0,0,0
for i = 1,#EI,4 do r,g,b = r+EI[i],g+EI[i+1],b+EI[i+2] end
return Color3.new(r/(#EI/4),g/(#EI/4),b/(#EI/4))
end
return AvgColorFinder
Yes, this is that small! In only 8 lines!
Or, if you prefer cleanliness then:
local AvgColorFinder = {}
local AssetService = game:GetService("AssetService")
function AvgColorFinder.GetAverageColor(Image:string,Size:Vector2)
local EI = AssetService:CreateEditableImageAsync(Image)
EI.Size = Size
local r,g,b = 0,0,0
local col = EI:ReadPixels(Vector2.zero,Size)
local cols = {}
for i = 1,#col,4 do
local r0,g0,b0 = col[i],col[i+1],col[i+2]
r += r0
g += g0
b += b0
end
r = r/(#EI/4)
g = g/(#EI/4)
b = b/(#EI/4)
local Color = Color3.new(r,g,b)
EI:Destroy()
return Color
end
return AvgColorFinder
Thanks to @kalabgs for showing me how I can optimize my code.
- Great job on this.
- Eh. (Tell me why?)
- Bad (Tell me how it’s bad or else your just jealous [of my scripting skillz])
0 voters
Okay, bye bye :3