How do I make a gradient from any one colour to another?

Hello. I am making a game and inside the game I have a script which takes the height of each part and applies a gradient from white to black.

This is my code:

function mapValue(value, low1, high1, low2, high2)
	local Fraction = (value - low1) / (high1 - low1)
	return math.floor((low2 + high2 - 1) * Fraction)
end

function colour()
	local high = math.huge * -1
	local low = math.huge

	for x=1, gridSize do -- determine highest and lowest points
		for y=1, gridSize do
			if workspace.map.points:FindFirstChild(x .. " " .. y).Position.Y > high then
				high = workspace.map.points:FindFirstChild(x .. " " .. y).Position.Y
			end
			if workspace.map.points:FindFirstChild(x .. " " .. y).Position.Y < low then
				low = workspace.map.points:FindFirstChild(x .. " " .. y).Position.Y
			end
		end
	end

	for k,v in pairs(workspace.map.fill:GetChildren()) do -- colour points relative to highest and lowest points
		local height = v.Position.Y
		local brightness = mapValue(height, low, high, 0, 255)
		v.Color = Color3.fromRGB(brightness, brightness, brightness)
	end
end

this gives me this:

is there any way I can choose any two colours and make the gradient go from one to the other rather than black to white? For example, make the lowest points orange and the highest points purple and everything in between.

You could use Color3:lerp. For example starting_colour:lerp(goal_colour, 0.6) would return a colour that is 60% of the way through the gradient. In your example, you may want to make this a function of altitude.

2 Likes

Thank you. Edited my code a bit and works perfect:

local lowColour = Color3.fromRGB(0,0,0)
local highColour = Color3.fromRGB(0,255,0)

function mapValue(value, low1, high1, low2, high2)
	local Fraction = (value - low1) / (high1 - low1)
	return ((low2 + high2) * Fraction)
end

function colour()
	local high = math.huge * -1
	local low = math.huge

	for x=1, gridSize do
		for y=1, gridSize do
			if workspace.map.points:FindFirstChild(x .. " " .. y).Position.Y > high then
				high = workspace.map.points:FindFirstChild(x .. " " .. y).Position.Y
			end
			if workspace.map.points:FindFirstChild(x .. " " .. y).Position.Y < low then
				low = workspace.map.points:FindFirstChild(x .. " " .. y).Position.Y
			end
		end
	end

	for k,v in pairs(workspace.map.fill:GetChildren()) do
		local height = v.Position.Y
		local x = mapValue(height, low, high, 0, 1)
		v.Color = lowColour:lerp(highColour, x)
	end
end
1 Like