How can I find the median of 2 color values using only code?

I’m trying to find a way to find the median of 2 color3 values using only code, but I have not found a way to do so.

I’ve tried using tweens and stopping it mid way to get the color value, but it’s inconsistent.

local tweenService = game:GetService("TweenService")
local debris = game:GetService("Debris")

function getMedianColor(color1,color2)
	local new = Instance.new("Part")
	new.Parent = workspace
	debris:AddItem(new,1)
	new.Color = color1
	local tween = tweenService:Create(new,TweenInfo.new(1),{Color = color2})
	tween:Play()
	task.wait(.5)
	tween:Pause()
	return new.Color
end

--Expected output = 0.5,0.5,0.5
print(getMedianColor(Color3.new(0,0,0),Color3.new(1,1,1)))

How can I make a consistent way to get the median color?

Try something like this:

local R1=0
local G1=0
local B1=0

local R2=1
local G2=1
local B2=1

local MedianColor = Color3.new((R1+R2)/2,(G1+G2)/2,(B1+B2)/2)

You could also try local MedianColor = (Color3.new(1,1,1)+Color3.new(0,0,0))/2, Though I am not sure if that works.

This is actually a pretty bad way to do it, I strongly suggest looking into Color3:Lerp()

4 Likes

After looking at this, yes, this is a better solution.

1 Like

I Actually never knew that there was a Color3:Lerp(), I only ever saw Vector3:Lerp() and CFrame:Lerp()

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.