10

Is there any way to check color is dark or ligh , in the sense black tone or white in flutter and get a boolean value true or false

Noobdeveloper
  • 405
  • 2
  • 6
  • 19
  • Does this answer your question? [How to decide font color in white or black depending on background color?](https://stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color) – Peter O. Jun 28 '21 at 22:52

2 Answers2

12

You can also use the approach to take the luminance and apply the threshold specified by the W3C. In dart code this can look like this:

Color getFontColorForBackground(Color background) {
  return (background.computeLuminance() > 0.179)? Colors.black : Colors.white;
}

More background information can be found here: https://stackoverflow.com/a/3943023/3041394

schlenger
  • 1,447
  • 1
  • 19
  • 40
8

To check whether color is dark or light, we will need to convert that color into its greyscale color. Formula to find grayscale of any color from its RGB value is:

grayscale = (0.299 * Red) + (0.587 * Green) + (0.114 * Blue)

And than check:

if(grayscale > 128){
    // color is light
}else{
    // color is dark
}
Kartik Patel
  • 557
  • 3
  • 11