Flutter how to bind color string - flutter

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...

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 can i convert hex color code to color name or rgb value?

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);

Paperjs subtract path to shape not working properly

With Paperjs, I try to subtract a path from a circle, but it is not working as expected. Here is my code:
// Create circle
var c1 = new Path.Circle(new Point(100, 70), 50);
c1.fillColor = 'red';
// Create path
var eraser = new paper.Path({strokeColor: 'black', strokeWidth: 20, strokeCap: 'round'});
eraser.add(new paper.Point(20, 20));
eraser.add(new paper.Point(100, 80));
eraser.add(new paper.Point(150, 150));
eraser.fillColor = 'white';
eraser.opacity = 0.6;
// Subtract
result = c1.subtract(eraser);
result.selected = true;
result.opacity = 0.8;
result.fillColor = 'pink';
It seems the path is seen as a polygone, not lines when subtracted:
Here is a jsFiddle : https://jsfiddle.net/Imabot/785ergpy/35/
Yes, this is because Paper.js do the boolean operation with the paths fill geometry, ignoring the stroke.
This is more obvious if you remove the stroke from your example (see this sketch).
What you need to do, if you want to subtract the stroke, is turning it into a path first.
Unfortunately, Paper.js doesn't have this feature yet, even if it's planned for a long time and exist as an experimental version (see this issue).
So you have to either use this experimental feature or use a vectorial drawing software like Adobe Illustrator, and export your stroke path as SVG for example, before using it with Paper.js.

How to set two different colors for a single string in itext

I have string like below, and i can't split the string.
String result="Developed By : Mr.XXXXX";
i can create a paragraph in itext and set font with color like below,
Font dataGreenFont = FontFactory.getFont("Garamond", 10,Color.GREEN);
preface.add(new Paragraph(result, dataGreenFont));
it set the green color to entire text result but i want to set color only for Mr.XXXXX part. How do i do this?
First this: you are using an obsolete version of iText. Please upgrade!
As for your question: a Paragraph consists of a series of Chunk objects. A Chunk is an atomic part of text in which all the glyphs are in the same font, have the same font size, color, etc...
Hence you need to split your String in two parts:
Font dataGreenFont = FontFactory.getFont("Garamond", 10, BaseColor.GREEN);
Font dataBlackFont = FontFactory.getFont("Garamond", 10, BaseColor.BLACK);
Paragraph p = new Paragraph();
p.Add(new Chunk("Developed By : ", dataGreenFont));
p.Add(new Chunk("Mr.XXXXX", dataBlackFont));
document.add(p);

Changing text line spacing

I'm creating a PDF document consisting of text only, where all the text is the same point size and font family but each character could potentially be a different color. Everything seems to work fine using the code snippet below, but the default space between the lines is slightly greater than I consider ideal. Is there a way to control this? (FYI, type "ColoredText" in the code below merely contains a string and its color. Also, the reason I am treating the newline character separately is that for some reason it doesn't cause a newline if it's in a Chunk.)
Thanks,
Ray
List<byte[]> pdfFilesAsBytes = new List<byte[]>();
iTextSharp.text.Document document = new iTextSharp.text.Document();
MemoryStream memStream = new MemoryStream();
iTextSharp.text.pdf.PdfWriter.GetInstance(document, memStream);
document.SetPageSize(isLandscape ? iTextSharp.text.PageSize.LETTER.Rotate() : iTextSharp.text.PageSize.LETTER);
document.Open();
foreach (ColoredText coloredText in coloredTextList)
{
Font font = new Font(Font.FontFamily.COURIER, pointSize, Font.NORMAL, coloredText.Color);
if (coloredText.Text == "\n")
document.Add(new Paragraph("", font));
else
document.Add(new Chunk(coloredText.Text, font));
}
document.Close();
pdfFilesAsBytes.Add(memStream.ToArray());
According to the PDF specification, the distance between the baseline of two lines is called the leading. In iText, the default leading is 1.5 times the size of the font. For instance: the default font size is 12 pt, hence the default leading is 18.
You can change the leading of a Paragraph by using one of the other constructors. See for instance: public Paragraph(float leading, String string, Font font)
You can also change the leading using one of the methods that sets the leading:
paragraph.SetLeading(fixed, multiplied);
The first parameter is the fixed leading: if you want a leading of 15 no matter which font size is used, you can choose fixed = 15 and multiplied = 0.
The second parameter is a factor: for instance if you want the leading to be twice the font size, you can choose fixed = 0 and multiplied = 2. In this case, the leading for a paragraph with font size 12 will be 24, for a font size 10, it will be 20, and son on.
You can also combine fixed and multiplied leading.
private static Paragraph addSpace(int size = 1)
{
Font LineBreak = FontFactory.GetFont("Arial", size);
Paragraph paragraph = new Paragraph("\n\n", LineBreak);
return paragraph;
}