Hello. Suppose I want to have something 10% pink, 10% black, 5% red, and 75% blue. How would this be approached?
Right now I have a folder full of number values for 11 components which I want blended. My goal is that when any of these values are updated, the weight * (1/11) is the percentage of influence that color has over a part color. Researching has not been particularly helpful and I’m not sure how lerp would work with several inputs.
local ingot = script.Parent.Parent
local chemical_makeup = ingot.chemical_makeup
local metals = require(game.ServerStorage.Systems.Crafting.metals)
function update_appearance ()
for component in chemical_makeup:GetChildren() do
ingot.Color = ingot.Color:Lerp(metals[component.Name].color,component.Value/100)
end
end
chemical_makeup.Changed(update_appearance())
while true do
update_appearance()
end
The first thing to do is make sure they all add up to 100%. Your example already does (10%, 10%, 5%, 75%) but let me know if you expect to have arbitrary inputs that might not add up to 100% and I’ll show you how to scale them.
Next, take each value and multiply it by that percentage. Add them up.
0.10*pink + 0.10*black + 0.05*red + 0.75*blue
Since Color3s can’t be multiplied by components, you’ll need to do it individually for each component.
This will probably work best. It’s a lot more dynamic.
Please ask any questions if you want anything explained or want to better understand how it works.
local function blend(inputs)
--- Returns a blend based on multiple Color3 values and percentages.
--- 'inputs' is a dictionary in the format of '[Color3] = amount'
---
--- Example:
--- {
--- [pink] = 0.1,
--- [black] = 0.1,
--- [red] = 0.05,
--- [blue] = 0.75
--- }
-- Get the sum of all of the amounts.
local sum = 0
for key, value in inputs do
sum += value
end
-- Set up some variables to accumulate the total of the blend.
local r, g, b = 0, 0, 0
for color, amount in inputs do
-- By dividing amount by sum, we guarantee
-- that all of the amounts add up to 1.
-- This can be referred to as 'normalized.'
local scale = amount/sum
r += color.R * scale
g += color.G * scale
b += color.B * scale
end
return Color3.new(r, g, b)
end