How do I set RGB values separately?

Okay, so my question may be a bit confusing.

I’m trying to set the color of a brick, but I want to be able to set the RGB values separately, when a player clicks the button.

Here’s an example of what I mean:
2019-06-27%20(2)
(i’m aware the script above doesn’t work)

3 Likes

You have to set a variable to a color first, then back to the original part:

local r = 1
local g = 0
local b = 0.5
local partColor = Color3.new(r,g,b)
part.Color = partColor

Note that it would be much simpler to do:

local partColor = Color3.new(1,0,0.5)
2 Likes

That’s great, but not exactly what I’m trying to do.

I’m basically trying to “add” the value to an already existing color.

For instance:
If the color was 0,0,255, and I want to add 255 R to 0,0,255, how would I do that?

local color1 = Color3.new(0,0,1)
local color2 = Color3.new(1,0,0)
local added = Color3.new(color1.r+color2.r,color1.g÷color2.g,color1.b+color2.b)

You can read separate values using…

part.Color.r

Note these are only readable values so you can take these and arrange them so you can update each one separately…


function UpdateR(part, Value)
     local B = part.Color.b
     local G = part.Color.g
     local R = Value
     part.Color = Color3.new(R/255, G, B)
end

Then you can just use that function for each color just changing the according values…

This would work, but part.Color.b is a range between 0 and 1 already, so you do not need to divide it:

function UpdateR(part, Value)
     local B = part.Color.b
     local G = part.Color.g
     local R = Value
     part.Color = Color3.new(R/255, G, B)
end
1 Like

Very true… I did not realize this at the time, just tested it… I will update the code…

It’s fine; RGBs always trick me like that, because you are never are sure I’d it’s 0 to 255 or 0 to 1.

So, here is a summary:

color.r
color.g
color.b

All are numbers between 0 and 1 (1 is 255, 0.1 is 25.5). They are read only, and can not be changed with code like

color.r = 1

Instead, you would have to do:

color = Color3.new(1,color.g,color.b)

I did something wrong, didn’t I?

local B = game.Workspace.part.Color.b
local G = game.Workspace.part.Color.g
local R = Value

function onClicked(playerWhoClicked)
	function UpdateR(part, Value)
     game.Workspace.part.Color = Color3.new(R/255, G, B)
	end
	end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

You have to call updateR() for it to run. You also have to define Value. Try this:


local B = game.Workspace.part.Color.b
local G = game.Workspace.part.Color.g
local R = 255 --Value you want

local function UpdateR(part)
     game.Workspace.part.Color = Color3.new(R/255, G, B)
end
local function onClicked(playerWhoClicked)
	UpdateR(script.Parent)
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)
1 Like

Okay, I did it, but it’s still not working…

local B = game.Workspace.clear.Color.b

local G = game.Workspace.clear.Color.g

local R = 0.5

function UpdateR(clear)

game.Workspace.clear.Color = Color3.new(R/255, G, B)

end

function onClicked(playerWhoClicked)

UpdateR(script.Parent)

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

Does the script have to be in the part, or can it stay in the button?

Is there a reason you can’t just do:

function onClicked(playerWhoClicked)

workspace.clear.Color = Color3.new(1,0,0)

end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

I don’t see why you want to do all this adding if it does nothing. What are effect are you trying to make? Do you want it to switch between 2 colors?

It’s complicated…

So I own a theater game, that has these buttons.

The purpose of these buttons is to change the color of the lighting. Clicking red adds red to the lighting, clicking green adds green to the lighting, and clicking blue adds blue to the lighting.

The way I had set up this effect is extremely convoluted.

Basically, there are three parts in the same position, but each has a different color pointlight. Red, green, and blue. When the “Red” button is clicked, the red pointlight is enabled. When the “Green” button is clicked, the green pointlight is enabled. So on and so forth: the colors mix.

I’m trying to implement the same effect for these “spotlights” I have. However, it’s not as simple with BrickColor, since BrickColor can’t mix like lighting can.

I did set the material of these spotlights to Plastic, so that the lighting can sorta change the color of the spotlight without interfering with BrickColor, but this often makes the spotlights seem dim, so I want to be able to use neon.

But I can’t use neon without going into BrickColor.

I hope this clears up everything. Sorry if it’s overly complicated.

So when the button is clicked do something like this,

Red.MouseClick:Connect(function()
     Part.Color = Color3.fromRGB(255, 0, 0)
end)

Blue.MouseClick:Connect(function()
     Part.Color = Color3.fromRGB(0, 0, 255)
end)

ect, I’m assuming this is what you asked for, which is simple?

1 Like

No, because the colors have to mix together, and unless I’m gonna have to pull some serious If that do this return mess, which I seriously don’t want to do given the complexity of my button scheme, I want a script that “adds” the color to the already existing color, similar to my pointlight method, if that’s even possible in Lua.

This whole thing’s a confusing mess… :sad:

The fact that near nobody here is using Color3.fromRGB makes me sad :crying_cat_face:

Please, use Color3.fromRGB. Makes a lot more sense, a lot easier to use and edit, and just looks a lot nicer :slight_smile:

1 Like

Is that gonna help my situation?

local function Color(Prop, Value, Color)
    local RGB = {R = Color.R, G = Color.G, B = Color.B}
    RGB[Prop:upper()] = Value
    return Color3.fromRGB(unpack(RGB))
end

RedButton.MouseClick:Connect(function()
    Part.Color = Color('R', 255, Part.Color)
end)

Idk did u want something like this? When u click the red button, it changes the Color.R to whatever color u chose? @HereComeDatEli

It’s just easier than having to do

Color3.new(100/255, 100/255, 100/255)

Color3.fromRGB(100, 100, 100)

See so much nicer :smiley: Please, that way you don’t have to deal with random decimals which make debugging this kinda stuff a lot harder