How to limit random color generation to not include dark colors like grey or black

In my game I have a system where u spawn with a randomly generated color.
The color is generated like so:

Color3.fromRGB(math.random(1,255),math.random(1,255),math.random(1,255))

How can I make it not include greys, blacks, and otherwise very dark colors?

This should work:

local MaximumRGB = 255
local MinimumRGB = 125
local R,G,B = math.random(MinimumRGB, MaximumRGB), math.random(MinimumRGB, MaximumRGB), math.random(MinimumRGB, MaximumRGB)

local FinalColor = Color3.fromRGB(R,G,B)
1 Like

I did this earlier but it limits the potential for certain colors such as a solid red which would be color3.fromRGB(255,0,0)

Ah, I understand.
Maybe you could make a system to detect if R, G and B are all below a certain threshold, and if so, randomize again until they all fit the requirements?

3 Likes

Instead of
Color3.fromRGB(math.random(1,255),math.random(1,255),math.random(1,255))

Make 3 variables, a, b and c and use math.random(1,255) for each one to assign their values.
Then pick a random number from 1 to 3 and use it to assign a, b or c as 0.
For example if the random value is 1 then make a = 0, if it’s 2 then b = 0, if it’s 3 then c = 0.
Then use
Color3.fromRGB(a, b, c))

1 Like

This wont leave out any colors?

I was potentially thinking of getting a random number math.random(255,255*3) (min could be less than 255 but thats just an example)

Then do this

local a = 0
local b = 0
local c = 0

for i = 1,math.random(255,255*3) do
    local choice = math.random(1,3)
    if choice == 1 then
        a += 1
    elseif choice == 2 then
        b += 1
    elseif choice == 3 then
        c += 1
    end
end

local color = Color3.fromRGB(a,b,c)

Is this logical to do? It is iterating a lot. I could possibly reduce it from iterating hundreds of times by making it add in increments of 5 instead.

Simple.

local r = Random.new()
local colorV = r:NextUnitVector()
local colorRGB = Color3.new(colorV.X,colorV.Y,colorV.Z)

Wouldn’t this work?

1 Like

Why make it that complicated? I did mess up though, the single value shouldn’t be 0, it should be 255

Here’s an RGB slider that shows the colour values: rgb color picker - Google Search

local a = math.random(1,255)
local b = math.random(1,255)
local c = math.random(1,255)

local pick = math.random(1,3) -- this section changes the random number generated to 255 for only one of the 3 values
if pick = 1 then 
    a = 255
elseif pick = 2 then 
    b = 255
else
    c = 255
end

local color = Color3.fromRGB(a,b,c)
2 Likes

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