How can i convert hex color code to color name or rgb value? - flutter

The thing is i have an hex color code which is pass to another screen and when i go to another screen i wanna convert that hex color code to color name or to rgb value is that possible? need some help here.
This is my hex color code value type #ff373334

the package color_models will help you convert a Hex color to CMYK, HSI, HSL, HSP, HSB, LAB, Oklab, RGB, and XYZ.
It's straightforward to use, and if you need a custom implementation, you can check the repository of the package to see how each model work to convert your Hex color to RGB.

Its simple
new Color(0xFF373334)
should work
or
Color(0xFF373334).red
Color(0xFF373334).green
Color(0xFF373334).blue

You can use it like this.
hexStringToColor(String hexColor) {
hexColor = hexColor.toUpperCase().replaceAll("#", "");
if (hexColor.length == 6) {
hexColor = "FF" + hexColor;
}
return Color(int.parse(hexColor, radix: 16));
}
Now you can call this function anywhere like this.
Color color = hexStringToColor(hexString);

Related

Generate a 2 random color on the same text

I'm new in a flutter. I can't figure out how to generate the 2 random colours on the same Text(). Can anyone help me out? Really appreciate.
String data = "+RM67.80";
var value = data.substring(0,1); // it will get the first character
After that you can check the condition if value == "+" apply green color otherwise apply red.

How to convert RGB to CIELAB or L* a* b* on Flutter

I have RGB value rgb[253, 216, 28] but i want to convert it to CIELAB or L*,a*,b* value in flutter how can i do it?
You can use package: flutter_color_model. Link: https://pub.dev/packages/flutter_color_models
So we will create a function to convert from RGB to CIELAB for convenient:
List<double> fromRGBtoCIELAB(int red, int green, int blue){
Color color = Color.fromRGBO(red, green, blue, 1);
LabColor labColor = LabColor.fromColor(color);
return [
double.parse(labColor.lightness.toString()),
double.parse(labColor.a.toString()),
double.parse(labColor.b.toString()),
];
}
Remember to add flutter_color_model to your pubspec.yaml file and import 'package:flutter_color_models/flutter_color_models.dart'; in your code ;)

Flutter how to bind color string

I need to bind the color code in a flutter. I have code like this.
color: Color(int.parse(widget.product.colors[i]))
Issue is previously i have full hex code in widget.product.colors[i] like 0xFF223263 but now i have just #223263. How can I bind this mean add before this
Mean something like
color: Color(int.parse(0xff$widget.product.colors[i]))
Mean if I need to add 0xff before the colors so it can show in-app
Remove the # then concatenate 0xff with the rest of the hex numbers
Like so
Color(int.parse("0xff${widget.product.colors[i].replaceAll("#", "")}"))
var col = widget.product.colors[i];
var stringCol = "ff" + col.substring(1, 7);
var intCol = int.parse(finalCol, radix : 16);
Color finalCol = new Color(intCol);
Use this finalCol wherever you want
if it's a text, then
Text('Hello World', style = TextStyle(color: finalCol));
Voila...

Unity is returning material color slightly wrong

I have this mini task in my game where you need to click trophies to change color of the wood on them. I have two arrays of colors, one is an array containing all possible colors and the other one contains four colors (the answer) as follows:
I've double checked that the colors are equal between the two arrays. For example the purple in Colors-array has exactly the same r, g, b & a values as the purple in the Right Order-array.
To check whether the trophies has correct color I just loop through them and grab their material color. Then I check that color against the Right Order-array but it's not quite working. For example when my first trophy is purple it should be correct, but it's not because for some reason Unity is returning slightly different material color than excepted:
Hope somebody knows why this is happening.
When you say, they are exactly same color, I assume you are referring rgb values from Color Inspector, which are not precise values.
Now I dont know what could be causing in different values of colors but
You can write an extension method to compare the values after rounding them to closest integer.
public static class Extensions
{
public static bool CompareRGB(this Color thisColor, Color otherColor)
{
return
Mathf.RoundToInt(thisColor.r * 255) == Mathf.RoundToInt(otherColor.r * 255) &&
Mathf.RoundToInt(thisColor.b * 255) == Mathf.RoundToInt(otherColor.b * 255) &&
Mathf.RoundToInt(thisColor.g * 255) == Mathf.RoundToInt(otherColor.g * 255);
}
}
usage:
Color red = Color.Red;
red.CompareRGB(Color.Red); // true;
red.CompareRGB(Color.Green); // false;
Hope this helps.
I would use a palette. This is simply an array of all the possible colors you use (sounds like you have this). Record, for each "trophy", the INDEX into this array, at the same time you assign the color to the renderer. Also, record the index for each "button", at the same time you assign the color to the renderer.
Then you can simply compare the palette index values (simple integers) to see if the color matches.

Is it possible to make colors invisible in the code?

For a game that I am currently making, I have a black background with many white dots, which are being used to represent stars. I have no need for the black color however, and I only wish for the stars to show. My question is this: Is it possible to completely hide the black color without using an image manipulation program, that is, using only code? Any help is appreciated.
This is the form:
// some rgba color representation
struct t_rgba {
uint8_t r, g, b, a;
};
bool SetToTransparentIfPixelIsBlack(t_rgba* const rgba) {
// is it black?
if (0 == rgba->r && 0 == rgba->g && 0 == rgba->b) {
// then set the alpha value to transparent:
rgba->a = 0;
}
}
although the image data you are using will be represented differently.