Alpha component for object._colors in XTK - xtk

Is it possible to have Alpha component for object._colors ?
var object = new X.object();
var c = object._colors;
var color = new Array(4);
c.add(color[0], color[1], color[2], color[4]);//Is this Allowed ..?
I am having the R,G,B,A values in color. where R= Red, G= Green, B= Blue and A = transparency component.
Is it possible to allow transparency/Alpha component ?

X.object has an attribute "opacity" (varying between 0 and 1) implemented in the mixin "displayable", so to use it from inside the framework use object._opacity and from outside "object.opacity=;" or "var myvar = object.opacity;" (respectively the setter and getter for opacity).
Other property you can use is object._visible (boolean) to choose if you display it or not.
Ricola3D

Related

Set the font size and color of an AmChart4 XYCursor's X and Y label

I've looked here:
https://www.amcharts.com/docs/v4/reference/xycursor/
and tried this
chart.cursor.fontSize = "14";
chart.cursor.fill = am4core.color("#ff0000");
chart.cursor.fontFamily = "verdana";
To style the (default white on black) element when the xycursor touches the X/Y axis (don't know what the correct name for this element is, hope you know which one I mean)
I would like to set the font size and family. Tried to set the color to red to see if it has to be set via the fill property, but also that doesn't work.
Created the cursor like this:
chart.cursor = new am4charts.XYCursor();
chart.cursor.xAxis = axis;
You have to set the tooltip properties inside the axis objects directly, as mentioned in the documentation. For example, to change the font, size and color in the category axis tooltip, modify the tooltip's label object:
categoryAxis.tooltip.getFillFromObject = false;
categoryAxis.tooltip.label.fill = "#ff0000"
categoryAxis.tooltip.label.fontFamily = "Courier New"
categoryAxis.tooltip.label.fontSize = 15;
Demo

Custom Fragment Shaders with SkiaSharp

I'm evaluating SkiaSharp for use in a project which allows for rendering some vector elements on top of a photo. SkiaSharp looks well suited to rendering the vector elements, however I also want to apply transformations to the image -- tone-curve adjustments like contrast or exposure, for example.
The ideal scenario would be a way to execute custom GLSL fragment shader code when rendering the rectangle containing the image (eg an SKColorFilter subclass that just wraps GLSL code, to be applied when calling DrawBitmap, or similar).
It would probably also work to have my existing GLSL code render to a texture, and then have SkiaSharp draw a rectangle using that texture as its contents. However, I can't see a way to do this without a GPU read-back, which feels like it would be prohibitively slow.
What's the best way forward here? More precisely: either, how can I apply transformations to the pixel data of an image, on the GPU, in SkiaSharp, or, what else could I use to provide SkiaSharp like vector rendering primitives and also allow GLSL fragment-shader-like pixel transformations?
You could have a look at some really early previews of the v2.84 series. They have a new SKRuntimeEffect that you can make use of.
Check out the preview feed: https://aka.ms/skiasharp-eap/index.json
You must have a look at the builds for the https://github.com/mono/SkiaSharp/pull/1321 PR:
v2.84.0-pr.1321.*
See this example: https://github.com/mono/SkiaSharp/issues/1319#issuecomment-640529824
// input values
SKCanvas canvas = ...;
float threshold = 1.05f;
float exponent = 1.5f;
// shader
var src = #"
in fragmentProcessor color_map;
uniform float scale;
uniform half exp;
uniform float3 in_colors0;
void main(float2 p, inout half4 color) {
half4 texColor = sample(color_map, p);
if (length(abs(in_colors0 - pow(texColor.rgb, half3(exp)))) < scale)
discard;
color = texColor;
}";
using var effect = SKRuntimeEffect.Create(src, out var errorText);
// input values
var inputs = new SKRuntimeEffectInputs(effect);
inputs.Set("scale", threshold);
inputs.Set("exp", exponent);
inputs.Set("in_colors0", new[] { 1f, 1f, 1f });
// shader values
using var blueShirt = SKImage.FromEncodedData(Path.Combine(PathToImages, "blue-shirt.jpg"));
using var textureShader = blueShirt.ToShader();
var children = new SKRuntimeEffectChildren(effect);
children.Set("color_map", textureShader);
// create actual shader
using var shader = effect.ToShader(inputs, children, true);
// draw as normal
canvas.Clear(SKColors.Black);
using var paint = new SKPaint { Shader = shader };
canvas.DrawRect(SKRect.Create(400, 400), paint);

Change bar color depending on value

I'm using chart-js/ng2-charts for an angular 2 app.
I can display a bar graph, but at the moment, all the bars are the same color. I'd like to have a different color depending on the value.
Can that be done?
After you create your chart, you can use the following function to loop through the dataset and change the color depending on the data value.
In this example, if the value is above a 50, the color changes to red.
var colorChangeValue = 50; //set this to whatever is the deciding color change value
var dataset = myChart.data.datasets[0];
for (var i = 0; i < dataset.data.length; i++) {
if (dataset.data[i] > colorChangeValue) {
dataset.backgroundColor[i] = chartColors.red;
}
}
myChart.update();
JSFiddle Demo: https://jsfiddle.net/6d0jsyxu/1/

How to visualize PhysicsWorld / CollisionShape with debug renderer?

My test scene for Urho3D/Urhosharp contains two boxes (see screenshot).
For both, the red and the green box I added a RigidBody and a CollisionShape.
The shape for the red box is a sphere and for the blue one a box. I've enabled debug visualisation by adding:
app.Engine.SubscribeToPostRenderUpdate(args => {
app.Renderer.DrawDebugGeometry(false);
});
However, this does not render the collision shapes but only the wireframe of the objects (green lines).
Here's the code for the blue node:
var floorNode = app.RootNode.CreateChild("Floor");
floorNode.Position = Vector3.Zero;
floorNode.Scale = new Vector3(10, 1, 10);
var floorComp = floorNode.CreateComponent<Box>();
floorComp.SetMaterial(Material.FromColor(new Color(0, 0, 1, 0.5f)));
var rigidBody = floorNode.CreateComponent<RigidBody>();
rigidBody.Mass = 0;
var collNode = floorNode.CreateComponent<CollisionShape>();
collNode.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
And for the red one:
var boxNode = app.RootNode.CreateChild("Box");
boxNode.SetScale(2f);
var boxComp = boxNode.CreateComponent<Box>();
boxComp.Color = Color.Red;
boxComp.SetMaterial(Material.FromColor(Color.Red));
var rigidBody = boxNode.CreateComponent<RigidBody>();
rigidBody.Mass = 0f;
var collNode = boxNode.CreateComponent<CollisionShape>();
collNode.SetBox(Vector3.One, Vector3.Zero, Quaternion.Identity);
collNode.SetSphere(3f, Vector3.Zero, Quaternion.Identity);
The collision works but I'd love to see the physics world because it makes it easier to experiment.
And here's the solution: the default debug output of the Urho3D renderer does not include the physics components. It has to be added manually.
app.Engine.SubscribeToPostRenderUpdate(args => {
// Default debug rendering.
app.Renderer.DrawDebugGeometry(false);
// Use debug renderer to output physics world debug.
var debugRendererComp = app.Scene.GetComponent<DebugRenderer>();
var physicsComp = app.Scene.GetComponent<PhysicsWorld>();
if(physicsComp != null)
{
physicsComp.DrawDebugGeometry(debugRendererComp, false);
}
});
For this to work the scene must of course have a debug renderer and a physics component:
app.Scene.GetOrCreateComponent<PhysicsWorld>();
app.Scene.GetOrCreateComponent<DebugRenderer>();
Then the result is as expected. In the image below the red box has got a spherical collision shape:
The nice thing I noticed: since the collision shape is a child node of the actual node it will scale together with the node. This means if the box or sphere (or whatever) is created with a size of Vector3.One it will always match the actual node size.

What is a good way to store color?

I started to make "game of life" and I thought, what if I could have more states than 1 or 0.
But then I need different colors. I want the colors to be linked to a grid/object (the grid is a class).
What is a good/decent way to store color pallets for fast/easy access?
My current less than ideal solution was to have 4 pointers to memory for each red, green, blue and alpha value.
In my class I had a function to set the color of value v to rgba:
SetColor(v, r, g, b, a) //Set v to the appropriate color values
I would like to keep this function to easily modify a color.
What I use is something really simple: 4 floats
struct Color {
float r, g, b, a;
};
Then, you can have something like a color pallete:
// a Palette of 8 colors:
using palette_t = std::array<Color, 8>
palette_t myPalette { /* ... */ };
Then, in your grid or object class, you can reference the color with an index:
struct Grid {
// lot's of code
private:
std::size_t colorIndex = 0;
};
But then you asked how to have easy access to the color (I guess the easy access is from within the Grid class)
There is a lot of solution that can exists, and most will depend of your project structure. This is one idea many others. I hope it will inspire you.
You can store a function that return the right color:
struct Grid {
// lot's of code
private:
std::size_t colorIndex = 0;
std::function<Color(std::size_t)> color;
};
And then, have something that create your grid correctly:
struct GridCreator {
GridCreator(const palette_t& aPalette) : palette{aPalette} {}
Grid create(std::size_t color) const {
Grid grid;
// create the grid
grid.color = [this](std::size_t index) {
return palette[index];
};
return grid;
}
private:
const palette_t& palette;
};
Then you got free access to your color palette without directly knowing that the palette exists from the Grid class.
Have an array of colors:
std::vector<std::array<unsigned char, 4>> palette {
{255, 0, 0, 255}, // red
{0, 255, 0, 255}, // green
{0, 0, 255, 255}, // blue
};
Then for each field store the index in the array (of type size_t). Example:
auto id = field[5][2];
auto color = palette[id];
auto r = color[0], alpha = color[3];
Changing a color is as simple as:
palette[id] = {255, 0, 0, 127};
For adding new colors, use:
palette.push_back({255, 0, 0, 127}).
Alternatively, you can define a simple struct so that you can use color.r, color.alpha etc. and write a constructor for easy color creation.
Mind this example is C++11 code.
Enums are perfect for Colors kind of structure.
More code readability.
Better compiler-time optimizations.
enum Color { red, green, blue };
Color r = red;
switch(r)
{
case red : std::cout << "red\n"; break;
case green: std::cout << "green\n"; break;
case blue : std::cout << "blue\n"; break;
}
For your special case.
You can store color for each point as a single integer.
uint32_t point_color = field[5][2].color;
unsigned char* color = (unsigned char*)point_color[id];
auto r = color[0], alpha = color[3];
/////
void SetColor(uint32_t& point_color,unsigned char r,
unsigned char g,unsigned char b,unsigned char a){
point_color=r | (b*(1<<8)) | (g*(1<<16)) | (a*(1<<24));
}
Pros of this structure
lesser messy.
faster bit operations only.
lesser memory.
Your question is dualistic:
What is a good way to store color?
what is a good/decent way to store color pallets?
If you really only want to store a colour, my advise would be not to store the colours as references, but to store the RGB values in one INT32 inside a class where you can get and set the Reg / Green / Blue values. Compare this with how .NET does this.
The advantage of this method is that it does no more space than using pointers and it is fast in get / set / comparisons.
A color pallette is a collection of colours. This is quite often used if you want to switch all used colours into a different colour. For instance if you want all light-blue colours in to misty-blue colours, and all fire-red colours into brick-red.
This is used by defining a palette as a pre-defined sized array or RGB values. Historically a palette contains 16, 64, or 256 values, or sometimes 2 if you only want black/white pictures.
If you use a palette, every dot in your grid has an index in your palette array and your grid has a current pallette. The RGB value of point X of grid G is then G.CurrentPallette[X.PaletIndex]. Changing all colours of your grid at once means switching to a different palette: G.CurrentPalette = otherPalette. This is a really fast and efficient way of changing the colours in your grid
Whether to use the RGB method or the Palette method depends on what you want:
Do you only want to use a subset of all possible colours, and do you want to change the complete subset into a different subset: use the Palette method
Do you want to change the colour of dots with a certain state, for instance change all yellow dots to green, use the palette method: change the color yellow in your palette to green
However, if you only want to change individual dots on your grid it is probably more efficient to use the RGB method.
any color value has an interval from 0 through 255 so you need 3 unsigned char variables and another one for alph. we make a whole thing, mapping them an a struct:
typedef struct Color
{
unsigned char _ucRed;
unsigned char _ucGreen;
unsigned char _ucBlue;
unsigned char _ucAlpha;
}COLOR;