Home > Net >  How to compare two colors for similarity/difference in Flutter
How to compare two colors for similarity/difference in Flutter

Time:11-29

I would like a program that does something like this: https://www.colortools.net/color_matcher.html

Anyone know how to do in flutter?

I expect a function that compares two colors.

CodePudding user response:

You can also compare two Colors by directly comparing their int value :

  bool compare(Color color1, Color color2) {
   return color1.value == color2.value;    
  }

final isTwoRedTheSame = compare(Colors.red, Color(0xFFFF0000));
final isRedEqualsGreen = compare(Colors.red, Colors.green);

print(isTwoColorsSame); // true    
print(isRedEqualsGreen); // false

CodePudding user response:

You can achieve comparison between two colors, is by comparing their degree of RGB, red, green, blue.

    bool compare(Color color1, Color color2) {
    return color1.red == color2.red &&
        color1.green == color2.green &&
        color1.blue == color2.blue;
  }

  final isColorsTheSame = compare(Colors.purple, Colors.purple);
  print(isColorsTheSame); // true

Note:

this will result in a full comparison of two colors, you can also compare only one of the three RGB each separately to get percentage of match.

  • Related