I'm rebuilding my app from java to flutter. I'm using firebase to store colors as integer values. In java I can use the following to convert rgb values to integer values:
colorInt = (255 << 24) | (color.red << 16) | (color.green << 8) | color.blue;
And I can use the following to convert from an integer value to rgb:
int r = (colorInt >> 16) & 0xFF;
int g = (colorInt >> 8) & 0xFF;
int b = colorInt & 0xFF;
How can I convert a Java integercolor to a flutter color and back?
An example, lets take RGB 154, 255, 147
In flutter this would be the result is 4288348051
(255 << 24) | (154 << 16) | (255 << 8) | 147;
(4278190080) | (10092544) | (65280) | 147;
(4278190080) | (10092544) = 4288282624;
(4278190080) | (10092544) | (65280) = 4288347904;
(4278190080) | (10092544) | (65280) | 147 = 4288348051
In java this would be -6619245
(255 << 24) | (154 << 16) | (255 << 8) | 147;
(-16777216) | (10092544) | (65280) | 147;
(-16777216) | (10092544) = -6684672;
(-16777216) | (10092544) | (65280) = -6619392;
(-16777216) | (10092544) | (65280) | 147 = -6619245;
You can retrieve color value and change it back to color like this : int value = int. parse(_storeColorValue); Color color = Color(value). withOpacity(1);
var myInt = int. parse("0x$hexString"); as a prefix of 0x (or -0x) will make int.
Step 1: Remove the # sign. Step 2: Add 0xFF at the beginning of the color code. Step 3: Place it inside the Color class like this Color(0xFFbfeb91).
Now, in the example, you’ve provided toString actually just creates a String representation of the current Color value. And since this object is a String, you can’t change it back to a Color with an as. Instead, you can parse the string into a value and construct a new Color object.
It is a 32-bit int value that represents your color. You can save it and then use it to create your new Color object. Color pickerColor = new Color (0xff443a49); int testingColorValue = pickerColor.value; String testingColorString = pickerColor.toString (); Color newColor = new Color (testingColorValue);
Flutter already has many MaterialColor available, but still, if you want to create your own MaterialColor with your own shades for that just follow the below approach. MaterialColor extends ColorSwatch which is kind of like a Map of colors. You can use ColorSwatch anywhere you could use a Color and get the 900 shade with the index into it [number].
For that, you could use the Color property value. It is a 32-bit int value that represents your color. You can save it and then use it to create your new Color object.
So, as @Ovidiu mentioned, Java uses signed 32 bit integers. Expanding on the code block they posted, we can create a full example which gives us what we need:
import 'dart:math';
int chopToJavaInt (int result) {
while (result > pow(2, 31)) {
result = result - pow(2, 32);
}
return result;
}
int javaIntColor(int r, int g, int b) {
var x = (g << 24) | (r << 16) | (g << 8) | b;
return chopToJavaInt(x);
}
void main () {
print(javaIntColor(154, 255, 147)); //-6619245
}
This solution is modular, maintains your original style from your Java code, and will return the correct value.
Java is using signed 32 bit int, so values are between -2^31 and 2^31. Once they reach 2^31, they continue from -1 towards -2^31.
Try applying this on the result you get in Flutter:
if (result > pow(2, 31)) {
result = result - pow(2, 32);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With