Get Color Name #55
-
Hello, the return type when the color is changed in DialogPicker is a material Color. This has no property of name, which I need to get to show to the user. I know that you have each color name because you display it in the dialog. How can I receive the picked color name? Is there a function to change from color to color name? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
|
Beta Was this translation helpful? Give feedback.
-
Hi Tomas @TomasWard1, Thank you for your question. Yes there are functions for this. The FlexColorPicker exposes almost all of its internal color tools via APIs. See for example With The To get the Material 2 Color name use It can return the Material 2 color name with, or without the index. All colors do not have a Material 2 color name, if it is not a Material 2 color, an empty string is returned. To get the color name of the color closest matching the color from a "Name That Color" list of 1566 named colors, use The returned color name is based on a Dart port of a JavaScript tool called 'ntc', short for "Name That Color". The javascript project and info about it can be found here http://chir.ag/projects/ntc. This function always finds a name for a passed in color, at least I have not yet been able to pass it color value where alpha is #FF and have it not return a name. How useful the name is I leave to others to debate that came up with the 1566 color names, but at least colors get a name which can often be useful. Color values that are "close" to each other, in the sense that the colors looks fairly similar to each other, result in the same name, even if the value is a bit different. Example// Use any color value.
final Color myColor = Color(0x66F57C00);
// If the color has other alpha value than #FF, like above where it
// is #66, then set alpha to #FF when using the functions, otherwise
// the name for the color is not found as value with alpha does not
// equal the named colors or material color, but we can of course
// find its equivalent with no alpha set.
// What is myColor's material color name, with index?
final String materialColorWithIndex =
ColorTools.materialName(myColor.withAlpha(0xFF)); // "Orange [700]"
// What is myColor's material color name, without index?
final String materialColorWithNoIndex =
ColorTools.materialName(myColor.withAlpha(0xFF), withIndex: false); // "Orange"
// What is myColor's "name that color" name?
final String namedColor =
ColorTools.nameThatColor(myColor.withAlpha(0xFF)) // "Gold Drop"
Hope this helps! If not feel free to elaborate your question further, and if it solved your issue/question, please feel free to close it. 😃 |
Beta Was this translation helpful? Give feedback.
-
Thank you, that was a perfect answer. Follow up: Do you have something similar but to get the appropriate text color? For example, if the color is dark blue, I want the text to be white, or if the color is yellow, I want the text to be black. You seem to use this logic when setting the color of the 'check mark' of a selected color. Could you tell me how to do it? Thanks!! |
Beta Was this translation helpful? Give feedback.
-
You are welcome Tomas, Yes there is indeed a feature to determine if a color used as background is better suited for dark (usually black) text or light (often white) color. Using
|
Beta Was this translation helpful? Give feedback.
-
You are the best person on the planet. Quick, concise answers. Incredible! keep it up. Thanks. |
Beta Was this translation helpful? Give feedback.
Hi Tomas @TomasWard1,
Thank you for your question. Yes there are functions for this. The FlexColorPicker exposes almost all of its internal color tools via APIs. See for example
ColorTools
:https://pub.dev/documentation/flex_color_picker/latest/flex_color_picker/ColorTools-class.html#static-methods
With
FlexColorPicker
package imported, you can use theColorTools
static functionsColorTools.materialName
andColorTools.nameThatColor
anywhere in your app on anyColor
value, without using theFlexColorPicker
. You just pass them aColor
and you get theString
name back.The
FlexColorPicker
uses these functions internally and provide them as exported tools as well. I use them and other color t…