There are several ways to do this. How you decide to do this will depend on how much you value the speed and simplicity or uniformity of perception. If you need it to be truly uniform, you will need to define RGB colors with a color profile, and you will need profile primaries so that you can convert to XYZ and then to LAB, where you can manipulate the L channel.
In most cases, you do not need to do this, and instead you can use a simple HSB model, such as Photoshop, in the information palette.
To do this, you simply imagine the line between the RGB point and the white point in 3D space and move your color along this line. In practical terms, you can simply create a parametric equation for this line and move the parameter.
import numpy as np def lighter(color, percent): '''assumes color is rgb between (0, 0, 0) and (255, 255, 255)''' color = np.array(color) white = np.array([255, 255, 255]) vector = white-color return color + vector * percent
As a percentage of 0.0, the same color will be returned, and 1.0 will return to white. Everything between them will be a lighter shade of the same shade. This should give you results that are consistent with the implementation of Photoshop HSB, but will be device dependent and may not be completely uniform.
If you have RGB [200, 100, 50] and fit in a percentage value of 0.50, it should return at least 20º of RGB[ 227.5 177.5 152.5] Photoshop.
It is easy to do this without numpy, but convenient operations with elements.
Edit based on comment:
I do not suggest that you do this if you do not know that you really need to do this. But if you want to convert to LAB, you can without any problems. Most importantly, you need to know what color space you need for RGB numbers, or you need to make some assumptions about their meaning. Since sRGB is pretty standard on the Internet, I will assume that here.
Conversions are not so difficult, but it’s easy to make mistakes. Fortunately, there is a nice colormath module with good documentation: https://github.com/gtaylor/python-colormath
Using this, you can convert between sRGB and LAB as follows:
from colormath.color_objects import sRGBColor, LabColor from colormath.color_conversions import convert_color sRGB = sRGBColor(126, 126, 126, is_upscaled=True)
Laboratory
now represents a color with the Luminance lab.lab_l channel, which can be moved up or down between black (0) and white (100). This should be more susceptibly consistent than HSB (but, depending on your application, it may not be enough to guarantee operation).
You can just change lab_l and then convert back:
lab.lab_l = 80 new_sRGB = convert_color(lab, color_objects.sRGBColor).get_upscaled_value_tuple()
new_sRGB now [198, 198, 198] . colormath took care of the lighting and gamma problems for you.