Increase color hue

Asked

Viewed 148 times

1

I have for example the following color in Hex #FAC328 and would like, via code, to increase its tone to another 10% staying this way

inserir a descrição da imagem aqui

It is possible to do this via javascript?

  • My 20% raise was #E1AF24 for #FFD22B, how you did the cover bill this raise?

  • It would not be easier to work with RGB colors and then convert to hexadecimal, making it easier to calculate the shades?

  • Leonardo I’m sorry, but in fact it’s 10% vo edit rs

  • @Victorlaio I have tried but not succeeded via rgb

2 answers

2

I set up this function that takes the HEX converts to DEC calculates the gain and converts again to HEX

hex -> without the #

ganho -> percentage from 0 to 100

return you can edit according to need.

function ganho(hex, ganho) {
	var r = Number.parseInt(Number.parseInt(hex.substring(0,2), 16) * ((ganho / 100) + 1)).toString(16).padStart(2, '0');
	var g = Number.parseInt(Number.parseInt(hex.substring(2,4), 16) * ((ganho / 100) + 1)).toString(16).padStart(2, '0');
	var b = Number.parseInt(Number.parseInt(hex.substring(4,6), 16) * ((ganho / 100) + 1)).toString(16).padStart(2, '0');

	return r + g + b;
}

console.log(ganho('101010', 10));

  • I could already understand the logic, thank you, your code gives a different return than the expected result, but I was able to solve following the post that Victor went through, anyway it’s the same logic, thanks for the help :D

0

Using the filter brightness I got the following result.

const dark = document.getElementById("dark");

dark.addEventListener("click", function() {
  let div = document.createElement("div");

  div.style.filter = "brightness(95%)";
  document.body.append(div);
});
div {
  background-color: #FAC328;
  width: 100px;
  height: 50px;
  margin-bottom: 2px;
}

div:nth-child(2) {
  background-color: #E1AF24;
}

button {
  margin: 10px 0;
}
<div>#FAC328</div>
<div>#E1AF24</div>
<button id="dark">GENERATE</button>

The base value of 100% represents the current color of the element.

  • good solution, but is it possible I get the result of the generated color? because in my case I need to discover the hexadecimal color.

  • 1

    I looked for a solution and I believe this is exactly what you are looking for: https://stackoverflow.com/questions/5560248/programmatically-lighten-or-darken-a-hex-color-or-rgb-and-blend-colors

  • there have some ideas yes, I managed to solve thank!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.