Answer the question
In order to leave comments, you need to log in
How can I change the contrast of a color by a given number on a scale from 1 to 21?
It was possible to find formulas that help calculate the contrast in the range from 1 to 21 between two colors, but a slightly different task appeared.
Is there a formula that will help change the contrast by a given number? For example, there is a text color that is in contrast with the background color by 3 units out of 21 and you need to change the color so that 3 becomes, for example, 4.5
// Вот так мы получаем яркость цвета
function getLuminance(rgb) {
rgb = rgb.map((val) => {
val /= 255;
return val <= 0.03928 ? val / 12.92 : ((val + 0.055) / 1.055) ** 2.4;
});
return Number((0.2126 * rgb[0] + 0.7152 * rgb[1] + 0.0722 * rgb[2]).toFixed(3));
}
// А эта функция получает контрастность между двумя цветами
function getContrastRatio(foreground, background) {
const lumA = getLuminance(foreground);
const lumB = getLuminance(background);
return (Math.max(lumA, lumB) + 0.05) / (Math.min(lumA, lumB) + 0.05);
}
Answer the question
In order to leave comments, you need to log in
You can try converting RGB to another color model like HSV or HSL or AHSL (or something like that). In these models, hue and brightness are separate independent coordinates (like red and blue in RGB), and you can change the brightness without changing the hue. Then back to RGB.
Well, how to change the brightness of pixels is a separate issue. Most likely, choose some kind of "average" brightness, and make everything that is darker than it even darker, and everything that is brighter - even brighter. More or less like this.
you translate the color into HSL
and change the lightness-darkness - this is what determines the contrast
, while the chroma-grayness and tone remain unchanged
here in the top row tone at the
bottom left chroma-grayness at the
bottom right lightness-darkness
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question