I have been working on this for about a week now and I still have no idea of how to DYNAMICALLY add a colorstop to a gradient fill.
I can fill an object with a gradient fill, but how do I add a new colorstop to it after it has been created?
circle.setGradient('fill',
{
type: 'linear',
x1: 0,
y1: -circle.height / 2,
x2: 0,
y2: circle.height / 2,
colorStops:
{
0: '#000',
1: '#f00'
}
});
canvas.add(circle);
Assuming I have the above code, how can I add a third colorStop (e.g. 0.5: 'blue') dynamically, after the fill has been created?
Help?
Related
How can I change the extent of a series of images?
Let's say we have (0, 100, 0, 200, 0, 300). I just want to change to (0, 200, 0, 100, 0, 300). It seems like rotate the volume.
Cheers
Swift 5.5, iOS 15
I have box I build using SCNBox like this using SceneKit.
let colors = [UIColor(red: 1, green: 0, blue: 0, alpha: 0.4), // front
UIColor(red: 0, green: 1, blue: 0, alpha: 0.4), // right
UIColor(red: 0, green: 0, blue: 1, alpha: 0.4), // back
UIColor(red: 1, green: 1, blue: 0, alpha: 0.4), // left
UIColor(red: 1, green: 140/255, blue: 0, alpha: 0.4), // top
UIColor(red: 1, green: 1, blue: 1, alpha: 0.4)] // bottom
let sideMaterials = colors.map { color -> SCNMaterial in
let material = SCNMaterial()
material.diffuse.contents = color
material.locksAmbientWithDiffuse = true
return material
}
cubeGeometry = SCNBox(width: 1.0, height: 1.0, length: 1.0, chamferRadius: 0.2)
cubeGeometry.materials = sideMaterials
And I am trying to figure out a way to see which face I hit when I click on it, so I using a hitTest function.
That produces an array of hits that I can look at. I tried looking at hit.geometryIndex, but that doesn't give me what I want? I want to be able to identify which of the six sides I tapped on.
I tried this answer that looked very promising, but it sadly it isn't reliable.
Identify face of a cube hit on touches began in Swift - SceneKit
hit.faceindex looks more promising to me, but how can I find out what the index values have been set too? The box is made up of triangles? Can I assume I got 12 face indexes given I got two per side?
How can I list the face index numbers?
Using an example from another post, I'm adding a color bar to a scatter plot. The idea is that both dot hue, and colorbar hue, should conform to the maximum and minimum possible, so that the colorbar can reflect the range of values in the hue:
x= [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200]
y= [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200]
z= [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 255]
df = pd.DataFrame(list(zip(x, y, z)), columns =['x', 'y', 'z'])
colormap=matplotlib.cm.viridis
#A continuous color bar needs to be added independently
norm = plt.Normalize(df.z.min(), df.z.max())
sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm)
sm.set_array([])
fig = plt.figure(figsize = (10,8), dpi=300)
ax = fig.add_subplot(1,1,1)
sb.scatterplot(x="x", y="y",
hue="z",
hue_norm=(0,255),
data=df,
palette=colormap,
ax=ax
)
ax.legend(bbox_to_anchor=(0, 1), loc=2, borderaxespad=0., title='hue from sb.scatterplot')
ax.figure.colorbar(sm).set_label('hue from sm')
plt.xlim(0,255)
plt.ylim(0,255)
plt.show()
Note how the hue from the scatterplot, even with hue_norm, ranges up to 300. In turn, the hue from the colorbar ranges from 0 to 255. From experimenting with values in hue_norm, it seems that matplotlib always rounds it off so that you have a "good" (even?) number of intervals.
My questions are:
Is which one is showing an incorrect range: the scatterplot, the scatterplot legend, or the colorbar? And how to correct it?
How could you retrieve min and max hue from the scatterplot (in this case 0 and 300, respectively), in order to set them as maximum and minimum of the colorbar?
Do you really need to use seaborn's scatterplot(). Using a numerical hue is always quite messy.
The following code is much simpler and yields an unambiguous output
fig, ax = plt.subplots()
g = ax.scatter(df['x'],df['y'], c=df['z'], cmap=colormap)
fig.colorbar(g)
I am making a pie chart that looks like this.
I want to make multiple pie charts for different sets of data and keep the colours fixed to the legend names, but when the order changes, the colour scheme follows the order.
Is there a way to pass a dict into the chart to fix the colours to specific items?
[]
You cannot pass a dictionary with your colors, but you can specify the colors manually, set sort to False and pass the values always in the same order, e.g.
import plotly
fig = {
'data': [{'labels': ['Residential', 'Non-Residential', 'Utility'],
'values': [19, 26, 55],
'type': 'pie',
'sort': False,
'marker': {'colors': ['rgb(255, 0, 0)',
'rgb(0, 255, 0)',
'rgb(0, 0, 255)']
}
}]
}
fig = {
'data': [{'labels': ['Residential', 'Non-Residential', 'Utility'],
'values': [100, 10, 25],
'type': 'pie',
'sort': False,
'marker': {'colors': ['rgb(255, 0, 0)',
'rgb(0, 255, 0)',
'rgb(0, 0, 255)']
}
}]
}
plotly.offline.plot(fig)
I have a 122 * 16 image.
It has seven colors, and I use only one color per block.
But, loadGraphic() in the FlxSprite class just uses width and height.
How do I use certain position of image?
For example, if I choose 0, loadGraphic() picks up the position width, height(0 to 15, 16), choose 1, (17 ~ 31, 16)
Could you use an animation but change the current frame manually? Something like this:
var sprite = new FlxSprite(100, 100);
sprite.loadGraphic("assets/image.png", true, 122, 16);
sprite.animation.add("my_animation", [0, 1, 2, 3, 4, 5, 6], 0, false);
sprite.animation.play("my_animation", true, 0);
//sprite.animation.paused = true;
sprite.animation.frameIndex = 2; // The frame you want to display