Setting the brightness and saturation of a rgb table not working.

Post Reply
LC Creations
Member
Posts: 159
Joined: Mon Feb 18, 2019 02:53
GitHub: LandonAConway

Setting the brightness and saturation of a rgb table not working.

by LC Creations » Post

So, I need to set the brightness AND saturation of a rgb table but it is not working. I have been on google trying to find answers on how to do this but only answers how to convert HSL to RGB completely. What I want is to convert an already existing RGB value's saturation and brightness. Here is some code I have already been working on.

Code: Select all

--sets the saturation of an rgba table. amount is 0-100.
function saturation(rgba, amount)
  local increase = (255 - ((amount / 100) * 255))
  local t = {}
  t.r = (max((rgba.r + increase)));
  t.g = (max((rgba.g + increase)));
  t.b = (max((rgba.b + increase)));
  t.a = rgba.a;
  return t
end

--sets the brightness of an rgba table. amount is 0-100.
function brightness(rgba, amount)
  local decrease = 255 - ((amount / 100) * 255)
  local t = {}
  t.r = (max((rgba.r - decrease)));
  t.g = (max((rgba.g - decrease)));
  t.b = (max((rgba.b - decrease)));
  t.a = rgba.a;
  return t
end

--see example of this being used below.
function saturation_and_brightness(rgb,sat,brightness)
  local _saturation = saturation(rgb,sat)
  local result = brightness(_saturation,brightness)
  return result
end

--but if I do this, it does not work. I will not just be using 50%. 50% is an example.
local desired_result = saturation_and_brightness({r=127,g=0,b=255},50,50)
I want the 'desired_result' to be like this. Pay attention to the "h" and "v" values in the window shown in the image below (saturation is horizontal while brightness is vertical):

Image

The idea is to add 50% white to purple then add 50% black. The result should be the above. This is what would happen in photoshop or Gimp (as seen below) while adding a plain purple layer (or any other color), then above it, a white layer 50% transparent, then above that, a black layer 50% transparent. I want to re-produce this effect on a rgb table.

Image
cdb_ac3a146dcafb

ShadMOrdre
Member
Posts: 1118
Joined: Mon Dec 29, 2014 08:07
Location: USA

Re: Setting the brightness and saturation of a rgb table not working.

by ShadMOrdre » Post

LC,

You state that you want to start with 50% whiteness, add purple, then add 50% blackness. This sounds more like a combinatation of alpha layers.

In RGB, lightness/darkness are fully new RGB values. 0,0,0 is black / white, while 255,255,255 is white / black. Hue is determined by the actual combinations of the RGB values, but lightness / darkness is either a lowering or raising of the combined RGB values. Lightness would apply to the whole table, giving you a single hue of purple, because the lightness / darkness settings essentially cancel each other.

You'll need at minumum 2 images / tables to accomplish this. The two tables include alpha gradients applied to white or black, respectively, while the purple could be a single pixel, applied to the table(s) of gradients.

Shad

Post Reply

Who is online

Users browsing this forum: Blockhead and 6 guests