Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension for text coloring based on luminance #128

Open
nogipx opened this issue Apr 10, 2021 · 0 comments
Open

Extension for text coloring based on luminance #128

nogipx opened this issue Apr 10, 2021 · 0 comments

Comments

@nogipx
Copy link

nogipx commented Apr 10, 2021

extension TextColor on Color {
  static Color Function(Color) luminanceDyer({
    Color light = Colors.white,
    Color dark = Colors.black,
  }) => (color) => color.computeLuminance() > 0.5 ? dark : light;

  Color dye(Color Function(Color) dyer) => dyer(this);

  Color luminance({
    Color light = Colors.white,
    Color dark = Colors.black,
  }) => TextColor.luminanceDyer(light: light, dark: dark).call(this);
}

void main() {
  final dyer = TextColor.luminanceDyer(
    light: Colors.white,
    dark: Colors.black,
  );
  Colors.white.dye(dyer); // Colors.black
  Colors.black.dye(dyer); // Colors.white
  Colors.greenAccent.dye(dyer); // Colors.black
  Colors.green.dye(dyer); // Colors.white
}

class ExampleWidget extends StatelessWidget {
  final Color? color;

  const ExampleWidget({
    Key? key,
    this.color,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: color?.luminance(
        light: Colors.white,
        dark: Colors.black87,
      ),
    );
  }
}
@nogipx nogipx changed the title Extension for text coloring Extension for text coloring based on luminance Apr 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant