Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

html - How can I get the color halfway between two colors?

I have two colors:

#15293E
#012549

How can I find the color that is half way in between them? Is there some way to do this calculation?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As Mr Lister just said, it is easy to automate the calculation with any programming language :

  1. Separate your two colors into their 3 color numbers for Red, Green, Blue : (r1,g1,b1) and (r2,g2,b2).
    • For example #15293E, #012549 become ("15", "29", "3E"), ("01", "25", "49")

  2. Convert each color string into an integer, specifying explicitly that you are parsing a hexadecimal-based representation of a number.
    • For example ("15", "29", "3E") becomes (21, 41, 62)

  3. Calculate the average (r',g',b') = ( (r1+r2)/2, (g1+g2)/2, (b1+b2)/2 ).
    • For example ( (21+1)/2, (41+37)/2, (62+73)/2) = (11, 39, 67)

  4. Convert them again to strings , specifying explicitly that you are generating two-digit hexadecimal representations (pad with a zero when necessary).
    • For example (11, 39, 67) -> ("0B", "27", "43")

  5. Concatenate a hash character followed by the 3 strings
    • For example ("0B", "27", "43") -> "#0B2743"

Edit : Implementation is not "very easy" as I initially stated. I took the time to write the code in several languages on Programming-Idioms .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...