I want to save a color chosen by user(it's a Xamarin.Forms.Color) in my local database, so I saved it as string using ToString method, the saved color is in this syntax: [Color: A=1, R=1, G=0.400000005960464, B=1, Hue=0.833333313465118, Saturation=1, Luminosity=0.699999988079071]
I want to retrieve it from the data base but I have to convert it back to Xamarin.Forms.Color (it has method like Color.FromHex or Color.FromHlsa or Color.FromRgb...)
Can anyone tell my the best way to convert it back?
Thanks
You can store the RGB values in your local database and then convert those integers back with Color.FromRBG(n,n,n); or you can store a string and do it with Color.FromHex("#"+ localDBProperty); Here is a reference link:
https://developer.xamarin.com/api/member/Xamarin.Forms.Color.FromHex/p/System.String/
Xamarin.Forms Colors can be created either from ARGB, or from AHSL. The conversion from one mode to the other is made automatically, so you never have to save both format in your DB (you're storing ARGBHSL).
now, there are plenty of ways to create the color from a subset of those values:
public Color(double r, double g, double b, double a);
public static Color FromHex(string hex); //one of the following: #rgb, #argb, #rrggbb, #aarrggbb, the leading # is optional
public static Color FromUint(uint argb);
public static Color FromRgba(int r, int g, int b, int a);
public static Color FromRgb(int r, int g, int b);
public static Color FromRgba(double r, double g, double b, double a);
public static Color FromRgb(double r, double g, double b);
public static Color FromHsla(double h, double s, double l, double a = 1d);
I'm quite sure you'll find either a constructor or a factory method in those that fits your needs.
Related
How can I read data 4D from Excel file for MILP model in Cplex?
I want to know how to define this parameter and apply in constraint
enter image description here
enter image description here
You can do that with a tuple set.
.mod
range A=1..2;
range B=1..3;
range C=1..4;
range D=1..2;
tuple someTuple{
key int a;
key int b;
key int c;
key int d;
int value;
};
{someTuple} someSet = ...;
int v[a in A][b in B][c in C][d in D]=item(someSet,<a,b,c,d>).value;
dvar int X[A][B][C][D];
subject to
{
forall(a in A,b in B,c in C,d in D) X[a][b][c][d]==v[a][b][c][d];
}
assert forall(a in A,b in B,c in C,d in D) X[a][b][c][d]==a*b*c*d;
.dat
SheetConnection sheet("write4Darray.xlsx");
someSet from SheetRead(sheet,"A1:E48");
How to Convert a string in float up to 3 decimal places in Arduino?
My string is
23.455 but when I convert it into float by toFloat() method it gives 23.45 and removes last digit.
void setup ()
{
Serial.begin (115200);
float f = atof ("23.455"); // convert to float
Serial.println (f, 3); // print with 3 decimal places
} // end of setup
void loop ()
{
} // end of loop
Output:
23.455
I found that.. float holds the whole value up to 3 decimal places but it just print up to 2 decimal places so the float value can be use for calculation
This may be useful. ArduinoFloatToString
I have this UML class and I don't really get why there are 2 setters along with constructors...? How to write that class in code?
I don't have enough reputation to post images but on the diagram the class looks something like:
Angle
-degrees: int
-minutes: int
-seconds: double
-set(degrees: int, minutes: int, seconds: double): void
-set(fractionalDegrees: double): void
+Angle()
+Angle(fractionalDegrees: double)
+Angle(degrees: int, minutes: int, seconds; double)
There's really no reason to have the setters in there like this, but essentially the UML is asking that you use the helper method set to change your private variables instead of doing it in the constructor. This would lead me to believe that each of the constructors just passes the variables along to the setter. So the flow would looks like
Create object (params) -> Constructor (params) -> set(params)
If you have no setters, but only attributes setting constructors, then your objects will be unchangeable. If the class has both setters and attributes setting constructors, it's objects are changeable. So, it is possible you need both - it is on you to decide.
About how to realize your methods:
void set(int degrees, int minutes, double seconds){
this. degrees = degrees;
this.minutes = minutes;
this.seconds = seconds;
}
You should decide how your representation will keep the sign. Let's suppose only degreees will keep it, and minutes and seconds will be shown always positive.
void set(double fractionalDegrees){
// separate the sign
int sign = Math.sign(fractionalDegrees);
double angle = Math.abs(fractionalDegrees);
// separate d:m:s
degrees = Math.floor(angle);
angle = angle - degrees;
angle = angle * 60;
minutes = Math.floor(angle);
angle = angle - minutes;
seconds = angle * 60;
// set the sign to degrees
degrees = degrees * sign;
}
As for constructors, they will simply call the appropriate setters.
public Angle(int degrees, int minutes, double seconds){
set(degrees, minutes, seconds);
}
Really, that class has serious problems: it has setters, but not getters or operations. So, it is practically unusable. You need probably the following:
double getFractionalDegrees();
int getDegrees();
int getMinutes();
float getSeconds();
Angle addTo(Angle anotherAngle);
Angle negate();
Angle back(); // +-180 degrees
String toString(); // you need to print them somehow, too
I am writing a tool in Java using Apache POI API to convert an XML to MS Excel. In my XML input, I receive the column width in points. But the Apache POI API has a slightly queer logic for setting column width based on font size etc. (refer API docs)
Is there a formula for converting points to the width as expected by Excel? Has anyone done this before?
There is a setRowHeightInPoints() method though :( but none for column.
P.S.: The input XML is in ExcelML format which I have to convert to MS Excel.
Unfortunately there is only the function setColumnWidth(int columnIndex,
int width) from class Sheet; in which width is a number of characters in the standard font (first font in the workbook) if your fonts are changing you cannot use it.
There is explained how to calculate the width in function of a font size. The formula is:
width = Truncate([{NumOfVisibleChar} * {MaxDigitWidth} + {5PixelPadding}] / {MaxDigitWidth}*256) / 256
You can always use autoSizeColumn(int column, boolean useMergedCells) after inputting the data in your Sheet.
Please be carefull with the usage of autoSizeColumn(). It can be used without problems on small files but please take care that the method is called only once (at the end) for each column and not called inside a loop which would make no sense.
Please avoid using autoSizeColumn() on large Excel files. The method generates a performance problem.
We used it on a 110k rows/11 columns file. The method took ~6m to autosize all columns.
For more details have a look at: How to speed up autosizing columns in apache POI?
You can use also util methods mentioned in this blog: Getting cell witdth and height from excel with Apache POI. It can solve your problem.
Copy & paste from that blog:
static public class PixelUtil {
public static final short EXCEL_COLUMN_WIDTH_FACTOR = 256;
public static final short EXCEL_ROW_HEIGHT_FACTOR = 20;
public static final int UNIT_OFFSET_LENGTH = 7;
public static final int[] UNIT_OFFSET_MAP = new int[] { 0, 36, 73, 109, 146, 182, 219 };
public static short pixel2WidthUnits(int pxs) {
short widthUnits = (short) (EXCEL_COLUMN_WIDTH_FACTOR * (pxs / UNIT_OFFSET_LENGTH));
widthUnits += UNIT_OFFSET_MAP[(pxs % UNIT_OFFSET_LENGTH)];
return widthUnits;
}
public static int widthUnits2Pixel(short widthUnits) {
int pixels = (widthUnits / EXCEL_COLUMN_WIDTH_FACTOR) * UNIT_OFFSET_LENGTH;
int offsetWidthUnits = widthUnits % EXCEL_COLUMN_WIDTH_FACTOR;
pixels += Math.floor((float) offsetWidthUnits / ((float) EXCEL_COLUMN_WIDTH_FACTOR / UNIT_OFFSET_LENGTH));
return pixels;
}
public static int heightUnits2Pixel(short heightUnits) {
int pixels = (heightUnits / EXCEL_ROW_HEIGHT_FACTOR);
int offsetWidthUnits = heightUnits % EXCEL_ROW_HEIGHT_FACTOR;
pixels += Math.floor((float) offsetWidthUnits / ((float) EXCEL_ROW_HEIGHT_FACTOR / UNIT_OFFSET_LENGTH));
return pixels;
}
}
So when you want to get cell width and height you can use this to get value in pixel, values are approximately.
PixelUtil.heightUnits2Pixel((short) row.getHeight())
PixelUtil.widthUnits2Pixel((short) sh.getColumnWidth(columnIndex));
With Scala there is a nice Wrapper spoiwo
You can do it like this:
Workbook(mySheet.withColumns(
Column(autoSized = true),
Column(width = new Width(100, WidthUnit.Character)),
Column(width = new Width(100, WidthUnit.Character)))
)
I answered my problem with a default width for all columns and cells, like below:
int width = 15; // Where width is number of caracters
sheet.setDefaultColumnWidth(width);
How should one best recode this example extension method to be generic for all numeric types?
public static float clip(this float v, float lo, float hi)
{ return Math.Max(lo, Math.Min(hi, v)); }
Thanks.
// IComparable constraint for numeric types like int and float that implement IComparable
public static T clip<T>(this T v, T lo, T hi) where T : IComparable<T>
{
// Since T implements IComparable, we can use CompareTo
if(v.CompareTo(lo)<0)
v=lo; // make sure v is not too low
if(v.CompareTo(hi)>0)
v=hi; // make sure v is not too high
return v;
}