Randomize swatch for a color passed to a function in Flutter - flutter

I have a function that gets Color as an argument. I want to generate a list of random swatches for that color within that function. For example, if the color passed is Colors.amber I want a list like:
[ Colors.amber[100], Colors.amber[800], Colors.amber[500], Colors.amber[900], Colors.amber[300]... ]
Is it possible? Any advice or help would be appreciated. Thank you in advance.

You can randomize only the list of "plus colors":
Flutter Colors
List<int> types = [50, 100, 200, 300, 400, 600, 700, 800, 900]
And randomize a number between 0..8
int get getRandomNumber => 0 + Random().nextInt(8 - 0);
Use to get the random color
Color? selectedColor = Colors.amber[types[getRandomNumber]];
Update:
Using this method extension, we can create a "workaround"
extension HexColor on Color {
/// String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".
Color fromHex(String hexString) {
final buffer = StringBuffer();
if (hexString.length == 6 || hexString.length == 7) buffer.write('ff');
buffer.write(hexString.replaceFirst('#', ''));
return Color(int.parse(buffer.toString(), radix: 16));
}
/// Prefixes a hash sign if [leadingHashSign] is set to `true` (default is `true`).
String toHex({bool leadingHashSign = true}) => '${leadingHashSign ? '#' : ''}'
'${alpha.toRadixString(16).padLeft(2, '0')}'
'${red.toRadixString(16).padLeft(2, '0')}'
'${green.toRadixString(16).padLeft(2, '0')}'
'${blue.toRadixString(16).padLeft(2, '0')}';
}
And using this function
Color getRandomColor(String selectedColor) {
final List<int> types = [50, 100, 200, 300, 400, 600, 700, 800, 900];
int getRandomNumber = 0 + Random().nextInt(8 - 0);
return Color.fromHex(selectedColor)[types[getRandomNumber]];
}
//true will return "#ffffff", false will return "ffffff"
Color _color = getRandomColor(Color.red.toHex(true));

Found a simple solution
Let color be the passed parameter. Now find the index of that color in the Colors.primaries list.
for(int i=0;i<Colors.primaries.length;i++){
if(color==Colors.primaries[i]){
colorIndex=i;
}
}
Use this index to generate the random swatch of the preferred color.
Colors.primaries[colorIndex][Random.nextInt(9) * 100]

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.

Dygraph custom color for grid lines

I want to have red color for grid lines having date (20Jul) in their x-axis label, but default(black) for time x-axis labels.
I have tried using "gridLineColor" in Dygraph options but it applies to all the grid lines at once.
view.chart = new Dygraph(panelForDygraph,
data, {
width: activeTab.getWidth() - 50,
height: panel.up().up().getHeight() - 150,
labels: channel,
labelsSeparateLines: true,
gridLineColor: 'lightgrey'
}
);
Here is what i want to do:
You can draw anything you like using an underlayCallback. Check out the highlighted-region page for a demo.
In your case you'd want something like:
view.chart = new Dygraph(
panelForDygraph,
data, {
width: activeTab.getWidth() - 50,
height: panel.up().up().getHeight() - 150,
labels: channel,
labelsSeparateLines: true,
gridLineColor: 'lightgrey',
underlayCallback: function(canvas, area, g) {
var x1 = g.toDomXCoord(new Date('2019-07-19'));
var x2 = g.toDomXCoord(new Date('2019-07-20'));
canvas.fillStyle = 'red';
canvas.fillRect(x1, area.y, 1, area.h);
canvas.fillRect(x2, area.y, 1, area.h);
}
}
);
You can adjust the width and positioning to taste.

ExtJS - Attributes changes are being ignored during drag action

We are using ExtJS 4.2.1. The width/height attributes of the img won't change in below example when element is dragged beyond the +/-5px y-coordinate. Changes to attribute 'show' are also ignored. However, element can be destroyed and re-created, but this is not desired.
[panel]
var dndLinkSprite = me.surface.add({
type: 'image',
x: bBox.x,
y: bBox.y,
width: 16,
height: 16,
src: '/link.png'
})
...
dragAction: function(panel, e, diff, dndConfig) {
var spriteLink = panel.dndLinkSprite;
if ( diff[1] > 5 || diff[1] < -5 ) {
spriteLink.setAttributes(height, 16);
spriteLink.setAttributes(width, 16);
} else {
spriteLink.setAttributes(height, 0);
spriteLink.setAttributes(width, 0);
};
}
Thanks for your help!
Solved - wrong syntax:
Instead of:
spriteLink.setAttributes(height, 0);
spriteLink.setAttributes(width, 0);
use:
spriteLink.setAttributes({width: 0, height:0} , true);
spriteLink.getEl().dom.setAttribute(height, 16);

Kendo line chart - series name with labels in two rows

I would like to know if it's possible to have line chart series names divided in two rows, with a label in addition (Group1 and Group2 from the photo). On the picture below you can see what I want to create.
If not, is it at least possible to divide series names in two rows (without labels)?
Here is the photo:
This is an example from a kendo ui homepage, here you can find the source code: link
I think you would need to play around with the legend width and the legend item visual:
DEMO
legend: {
position: "bottom",
width: 360,
item: {
visual: function (e) {
console.log(e);
var group = "";
var rect = new kendo.geometry.Rect([0, 0], [140, 50]);
if (e.series.name == "1. GOOG (close)"){
group = "Group 1: ";
rect = new kendo.geometry.Rect([0, 0], [200, 50]);
} else if (e.series.name == "3. AMZN (close)"){
group = "Group 2: ";
rect = new kendo.geometry.Rect([0, 0], [200, 50]);
}
var color = e.options.markers.background;
var labelColor = e.options.labels.color;
var layout = new kendo.drawing.Layout(rect, {
spacing: 5,
alignItems: "center"
});
var grplabel = new kendo.drawing.Text(group, [0, 0], {
fill: {
color: "#000"
}
});
var marker = new kendo.drawing.Path({
fill: {
color: color
},
stroke : {
color: color
}
}).moveTo(10, 0).lineTo(10, 10).lineTo(0, 10).lineTo(0, 0).close();
var label = new kendo.drawing.Text(e.series.name, [0, 0], {
fill: {
color: labelColor
}
});
layout.append(grplabel, marker, label);
layout.reflow()
return layout;
}
}
},
In the demo, I am checking the series name and adding the group label to the first and third series. The the legend width is set so that it wraps to a second line. NOTE: there is some trial and error to make this work with your data...

Add Gradient Pie Chart functionality to Highcharts-ng

I am using Highcharts-ng in an app. Everything is working fine. I've successfully added an additional "drilldown" attribute to the javascript file - great! But how do I add the code for a pie chart gradient fill?
$scope.chartConfig.options.colors = ["#55BF3B", "#DF5353", "#DDDF0D", "#7798BF"];
Highcharts.map($scope.chartConfig.options.colors, function (color) {
return {
radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 },
stops: [
[0, color],
[1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
]
};
});
My difficulty is the fact that it uses "Highcharts.map" and "Highcharts.Color"?
Highcharts.map is nothing more than simple for which can be replaced by for(var i = 0; i < colors.length; i++) { ... }.
Highcharts.Color create color object. You don't need to use that, just create two arrays for colors, one for starting colors, one for end colors:
var startColors = ["#55BF3B", "#DF5353", "#DDDF0D", "#7798BF"];
var endColors = ["#7798BF", "#DDDF0D", "#DF5353", '#55BF3B' ];
Then loop over them to set colors:
$scope.chartConfig.options.colors[0] = {
radialGradient: { cx: 0.5, cy: 0.3, r: 0.7 },
stops: [
[0, startColors[0]],
[1, endColors[0]]
]
}