3

I would like to be able to take a Color and convert it into a List<int> so like [0,255,255].

How do I do that?

2 Answers 2

6
List<int> getRGB(Color c) {
  return [c.red, c.blue, c.green];
}

To use

Color c = Colors.red;
print(getRGB(c));
Sign up to request clarification or add additional context in comments.

Comments

2

Use the .r, .b, .g properties which return fractional values (from 0 to 1). Multiply them by 255 and round to get the integer RGB values.

List<int> getRGB(Color c) {
  return [
    (c.r * 255).round(),
    (c.b * 255).round(),
    (c.g * 255).round(),
  ];
}

This method replaces the deprecated usage of .blue, .red, .green.

https://api.flutter.dev/flutter/dart-ui/Color-class.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.