How to convert Color to a list of RGB values in Dart & Flutter? - flutter

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?

List<int> getRGB(Color c) {
return [c.red, c.blue, c.green];
}
To use
Color c = Colors.red;
print(getRGB(c));

Related

How to convert string to color in Flutter

I want to convert the string to color but I can't,
My Code
String color = "0xffff5252";
Color result= Color(int.parse(color, radix: 16));
print("$result");
** I get the following problem**
Invalid radix-16 number (at character 1) 0xffff5252
You can try something like below
String color = "0xffff5252";
Color result= Color(int.parse(color));
print(result);
if youe motive is to define a color by first defining into string and then by parsing it, then there's a much simpler way to define color,
const Color primary = Color(0xFF92A3FD);
const Color secondary = Color(0xFF9DCEFF);
const Color thirdColor = Color(0xFFC58BF2);
const Color fourthColor = Color(0xFFEEA4CE);
const Color bgTextField = Color(0xFFF7F8F8);
const Color facebookColor = Color(0xFF3b5998);
const Color googleColor = Color(0xFFDB4437);
Try this hexToColor function:
// Construct a color from a hex code string, of the format RRGGBB.
Color hexToColor(String code) {
return Color(int.parse(code.substring(1, 6), radix: 16) + 0xFF000000);
}
This worked for me to store a color & use it as a profile avatar color.

Mudblazor pie/doughnut slice colors

I am exploring Mudblazor. I created a doughnut chart and it works fine. But I need to define the colors of the slices of the pie/doughnut. I see you can have a class for the entire chart, but how do set custom colors for each slice of the pie?
The default for a 3 slice pie is like blue, teal and gold. I need to change those colors and will need to be changed in code also.
The theming palette options don't seem to have options for this.
Thanks,
Michael
Edit: I figured it out. In case anyone else needs to do this. Example:
ChartOptions options = new ChartOptions();
protected override void OnInitialized()
{
string[] colors = { "#ff0000", "#00FF00","#0000ff" };
options.ChartPalette = colors;
}
Chart-add ChartOptions="#options":

Convert List of encoded Image to PDF

At first, I am converting a PDF file to Image file.
Then I make changes on the Image(converted PDF).
Now, I want to convert everything back to PDF and also have a copy in a base64 format of the PDF.
This is how I convert my PDF:
Future<List<List<int>>> imageFromPdfFileFullImage(
Future<PdfDocument> pdfFile) async {
int height = 0, width = 0;
final document = await pdfFile;
im.Image imImage;
for (int i = 1; i <= document.pagesCount; i++) {
final page = await document.getPage(i);
final pdfPageImage = await page.render(
width: page.width, height: page.height, format: PdfPageFormat.JPEG);
imImage = im.decodeJpg(pdfPageImage.bytes); // First issue in this line
height += imImage.height;
if (imImage.width > width) {
width = imImage.width;
}
//List<Image> buffer 'package:image/image.dart'
imageList.add(imImage);
//List<List<int>> 'package:image/image.dart' as im
//I want this encodeImaegList to be converted back to PDF later
encodeImageList.add(im.encodePng(imageList[i - 1]));
await page.close();
}
return encodeImageList;
}
This may be useful for those who's looking to convert some pdf files to images.
I hope you can help me on my problem too. Thanks guys!
Try this package, hope it helps
https://pub.dev/packages/pdf

TinyMCE get font size of selection

I'm currently building my own custom toolbar for TinyMCE, getting & setting the formats through the JS API.
For example, I can set the selected text to bold like this:
this._editor.formatter.toggle('bold');`
Afterwards I can get the format and set the state of my bold-button accordingly like this when the selection changes:
this.isBold = this._editor.formatter.match('bold');
To support font sizes I have a dropdown which applies the correct font size on change:
this._editor.formatter.apply('fontsize', {value: this.fontSize});
But now I need to be able to read the fontsize when the selection changes and I don't know how to achieve this. How can I read the fontsize of the current selection?
As a workaround I'm trying to match the format of the selected node against a list of supported font sizes.
const supportedFontSizes = ['10px', '11px', '12px', '14px', '16px', '18px', '20px', '24px'];
const defaultFontSize = '16px';
let foundFontSize = false;
let fontSize;
supportedFontSizes.some(size => {
if (editor.formatter.match('fontsize', { value: size })) {
fontSize = size;
foundFontSize = true;
return true;
}
return false;
});
if (!foundFontSize) {
fontSize = defaultFontSize;
}

Converting short colorVal = CellStyle.getBottomBorderColor () to RGB value while using Apache POI Java library

I am reading Excel sheet using Apache POI and writing it to a PDF using iText library.This has been achieved successfully but I am getting default black border for every cell that I write to PDF. So I need to get the cell border color using Apache POI which can be achieved using CellStyle class method getBottomBorderColor() which returns a short value.However I need a way to convert this value to RGB value so that while writing cell to PDF I can apply that RGB color value to the cell border.
The short value from CellStyle.getBottomBorderColor is an index of the color in the color palette of the workbook. This is an olld approach for storing colors from the old binary *.xls Excel format. So in apache poi there is only HSSFPalette which only should be used in HSSF and not more be used in XSSF.
In newer *.xlsx Excel formats, the color will either be stored directly as hex value or as reference to a theme color. So for XSSF there is XSSFCellStyle.getBottomBorderXSSFColor to get that color directly and not via index.
So unfortunately we have to differ both aproaches dependent on the kind of Excel workbook.
Example:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import java.io.FileInputStream;
class ExcelCellBorderColor{
public static void main(String[] args) throws Exception {
Workbook wb = WorkbookFactory.create(new FileInputStream("ExcelCellBorderColor.xlsx"));
//Workbook wb = WorkbookFactory.create(new FileInputStream("ExcelCellBorderColor.xls"));
String strrgb;
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
CellStyle style = cell.getCellStyle();
if (style instanceof XSSFCellStyle) {
XSSFColor xssfcolor = ((XSSFCellStyle)style).getBottomBorderXSSFColor();
if (xssfcolor != null) {
byte[] brgb = xssfcolor .getRGB();
strrgb = "R:"+String.format("%02X", brgb[0])+",G:"+String.format("%02X", brgb[1])+",B:"+String.format("%02X", brgb[2]);
System.out.println("Cell " + cell.getAddress() + " has border bottom color: " + strrgb);
}
} else if (style instanceof HSSFCellStyle) {
short colorindex = ((HSSFCellStyle)style).getBottomBorderColor();
HSSFPalette palette = ((HSSFWorkbook)wb).getCustomPalette();
HSSFColor hssfcolor = palette.getColor(colorindex);
if (hssfcolor != null) {
short[] srgb = hssfcolor.getTriplet();
strrgb = "R:"+String.format("%02X", srgb[0])+",G:"+String.format("%02X", srgb[1])+",B:"+String.format("%02X", srgb[2]);
System.out.println("Cell " + cell.getAddress() + " has border bottom color index: " + colorindex + ". This is " + strrgb);
}
}
}
}
wb.close();
}
}
you can archive this by using this color class
CTScRgbColor scrgb = (CTScRgbColor)ch;
int r = scrgb.getR();
int g = scrgb.getG();
int b = scrgb.getB();
color = new Color(255 * r / 100000, 255 * g / 100000, 255 * b / 100000);