ruby - Generate hex numbers based on percentage -


I see a way to generate gradual hexadecimal color based on ruby ​​with a percentage.

  0% = # 6da94a 50% = # decc30 100% = # ce2d4a   

Then programmatic hexadecimal values ​​are generated between those people.

So I have something like percent_to_hex (10) and that the hexadecimal value exceeds 10% with gradient gradient between 0% (green) and 50% (yellow) Will go

In fact, Trailston's answer is a minor mistake.

  (x + percent * 100 * (y - x)). Goal should be converted to   

:

  (X + percent / 100.0 * (Y - x)). Goal   

In addition to i.to_s (16) will be a problem if you have 0 255), as you can get the result like "ff0ff" I recommend using "% 02x"% i instead.

This is an absolute example:

  def percent_ to_hex (percent, start, stop)) color = [start, stop]. C. C.scan (/../). Map {| S | S.to_i (16)} end color_ int = colors.transpose.map do | X, y | (X + percent / 100.0 * (Y - X)). Round end colors_int.map {| I | "% 02x"% i} End ("") end    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -