Users can set the background-color of a button through a textbox that accept RGB hexadecimal notation: ff00ff
, ccaa22
, etc. So I need to set the text color to the opposite. Not sure about the terminology (opposite color) but the idea is assure readability.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I had the same issue once, and gathered information all over the internet also using Salman A answer, I came up with this function, it supports hex, rgb and rgba
but I don't think this is what you need, I found something more interesting, the below function will return white or black, it will decide witch one is more readable on the given color.
I don't take credit for any of this, I just got inspired and modified to my needs.
Sources: YIQ function explained
A bit late to the party, but IMO all text should either be light or dark. Colored text is for links.
Here's a coffeescript function I've written to decide which to use:
You could use this simple schema to achieve that goal. Just convert the color from RGB to HSV form. You can use this link . Then use this pseudo code;
Also you could divide [0,1] range into more parts. And instead of defining 2 colors (someBrightColor,someDarkColor) , define more colors. My suggested method is 'how bright is background color , thas text color must be dark, and vice versa'.
I linked another Question to this topic in the comments.
JS function to calculate complementary colour?
As Tejasva said you need to need to convert RGB to HSL, complement the Hue and convert it back.
I implemented the linked answer as a sample. Please upvote the original poster if this was helpful for you, because they provided the solution in fact.
Sample
http://jsfiddle.net/pLZ89/2/
Thanks for the post ZetCoby! Had to adjust your "color.substr(" array position to account for the initial '#'; then it worked great! you could also use a color.replace('#',''); within the if block...
You can invert the background color and use it as foreground color. The following algorithm produces results identical to the "Image > Adjustments > Invert" color command in Photoshop:
Note that this is not a bullet-proof solution. Colors that are close to 50% brightness and/or saturation will not produce sufficient contrast.
Demo on jsFiddle