LibGDX BitmapFont Markup Alpha Value - libgdx

Alright, so I have a bitmap font with markup enabled. The problem is, I need to set the global alpha value for a whole string of text without setting an individual alpha value for each color code.
For example, I have...
"[#0000ff]this is blue, [#990000]this is red"
I'd like the font text to fade into the background by setting an alpha value. Is there any way to do this without manually parsing color codes and sticking an alpha value into the brackets?
I've also tried adding custom colors with Colors.put(..), but that gets really clunky, as I have to set the alpha value to each color I'm using, with each line of text that I am drawing.

You'll need to use the BitmapFontCache class.
So for example, say your code currently looks like this...
font.drawWrapped(batch, text, x, y, wrapWidth, alignment);
Replace it with the following and you can control the alpha in the way you require...
BitmapFontCache cache = font.getCache();
cache.clear();
TextBounds bounds = cache.addMultiLineText(text, x, y, wrapWidth, alignment);
// This is the useful bit!
cache.setAlphas(alphaTransparency);
cache.draw(batch);
Note - If you're using distance field fonts then the shader most people use for them doesn't support alpha, but it's easy to fix it so it does. Let me know if you hit that problem.

Related

Detectron2 Mask-Rcnn keep same color segmentation for same object class

I am using detectron2 implementation of Mask-Rcnn on video, the problem is that on each frame, the segmentation color of a same object change.
Is there any parameter that can allow me to keep a single color for an object class.
I already tried detectron2.utils.visualizer.ColorMode(1) and it doesn't work
ColorMode(1) only has an effect if the metadata passed to the Visualizer has thing_colors defined. From the documentation for ColorMode,
SEGMENTATION= 1
Let instances of the same category have similar colors
(from metadata.thing_colors), and overlay them with high opacity. This
provides more attention on the quality of segmentation.
So, you need to add a list of pre-defined colors (one for each class) to your metadata. From here,
thing_colors (list[tuple(r, g, b)]): Pre-defined color (in [0, 255]) for each thing category. Used for visualization. If not given, random colors will be used.
Now, the actual colors used may not be the exact same ones as specified in thing_colors. In Visualizer.draw_instance_predictions(), each specified color is jittered by adding a random value to it, so the overlaid color is a slightly different one. This use of a random value means that you will still see class colors change between frames. Depending on the colors you specify, this change may or may not be visually obvious.
A simple solution to this might be to subclass Visualizer and override the _jitter() method so that it returns the color as is.
class MyVisualizer(Visualizer):
def _jitter(self, color):
return color
However, _jitter() is intended to be an internal method, so this is a hacky solution, and might break sometime down the line.
A better solution might be to override draw_instance_predictions() and customize the drawing to your needs.
Try to add the following lines
from detectron2.utils.visualizer import Visualizer , GenericMask , ColorMode
MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).thing_classes = clss
MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).thing_colors = [(255,255,0),(0,0,255),(0,255,0),(255,0,255),(156,225,242)]
class MyVisualizer(Visualizer):
def _jitter(self, color ):
return color
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2,instance_mode=ColorMode.SEGMENTATION)
v = v.draw_instance_predictions(outputs["instances"].to("cpu"))
img = cv2.resize(v.get_image()[:, :, ::-1], (1220, 780))

Can I change LibGDX Label lineHeight temporarily?

I have a bitmap font built with Hiero, which I'm using in scene2d Labels.
In a single Label instance I need to reduce the font's lineHeight value, but I'd like to leave the other Labels (which are using the same font) intact, so they should keep the default lineHeight of the font.
I've tried to simply adjust the value like this:
label.getStyle().font.getData().setLineHeight(localReducedValue);
However, this has modified all the Labels everywhere -- which, in retrospect, seems logical, since I'm modifying the LabelStyle itself.
Unfortunately something like label.setLineHeight(localReducedValue) doesn't exist, so at this point I see two possible solutions:
Create a copy of the font, set its lineHeight to the value I need, and create a separate LabelStyle with that font; or
Write a custom Label for myself which implements setLineHeight.
The first idea seems wasteful, the second is likely a bit complicated, so I'm hoping there's an easier way of achieving a temporary lineHeight in Labels.
Nathan Sweet, one of LibGDX's core developers has kindly suggested a solution, which is working perfectly:
Override Label#layout, set line height, call super.layout, set line height back. You need to use layout and not draw because layout computes and caches the glyph positions, draw just draws them.
You can change the LabelStyle of a single Label by doing something like this:
//skin is the skin that you use
Label myLabel = new Label(text, new LabelStyle(skin.get(LabelStyle.class)));
And then you can modify the style without affecting all the labels.

Add Html Text to Html5 Canvas

I got from a different Application a html formatted Text from user input.
(With font b br ul and so on, different fonts and colors in one fragment )
I would like to write this on a canvas.
like context.write("<b>Hello</b> World <font...>more text </font>");
how can i do this?
First, you'll have to make available to the canvas all the fonts you are using #font-face if needed.
Then you'll have to describe all the operations needed to draw the text, remember canvas is just a drawing surface. You'll have to iterate over these steps:
Specify the font to be used: (same syntax as the CSS font property)
context.font = "12px Arial bold"
Measure the string that will be drawn to know where to place the next one:
context.measureText(txt).width
Draw your text (fill or stroke):
context.fillText(txt, x, y)
This should help
https://developer.mozilla.org/en/Drawing_text_using_a_canvas
Though you would need to read the formats from the html and re-apply using this method.

Finding close colors of a given color

I´d like to know, how, through actionscript 3, to get an array of ARGB (hexadecimal) colors, that is close to a given color.
Example:
0xFF00FF00
A green.
How to get variations of green?
I´m trying to get some green colors of a bitmapdata.
I´ve tried to get it by making a loop getting the colors using getPixels32.
The problem is, I think the bits colors of each position are different from the bits of the bitmap rendered.
It´s for a pathfinder.
Each green bit sets a node in a map, as walkable.
So I need to know what are these colors to set it as walkable for the pathfinder.
Any suggestions?
RGB space is terrible for interpreting whether colors are similar to one another. A different color space that matches closer to human perception of color is HSV (hue saturation and value). Here are the steps you should follow:
Convert your value from RGB space to HSV (http://www.cs.rit.edu/~ncs/color/t_convert.html)
Modify the saturation and value to obtain different shades of the same green hue.
You can even modify the hue a little with a defined tolerance level you specify
Reconvert back to HSV to RGB
I believe technically..one color space is smaller than the other, meaning it is not always a 1:1 conversion - but it should serve your purpose.

I need a function to check each pixel then return the x and y for that pixel

I want to test each pixel in an image and check it's color if it is white then I must display
the corresponding (x,Y) for that pixel but I didn't find until now a function that help me
do something like that. So please if you have such function tell me.
thanks at all
If you are using matlab for image editting, the function for this purpose would be imread, i.e. if you have a MN image them imread would split it in a matrix of [MN*3], i.e. a 3 dimensional array representing RGB components of the image. Now if at a given X,Y coordinate, all the three i.e. Red, Green and Blue components have equal value, then that pixel is not color. If they have different values then it is coloured pixel. Using an if condition check and generate a new picture matrix accordingly. Display the new picture matrix.
I don't know if there is such a function. maybe in some deep graphics toolkit? it'll certainly not be part of a basic language framework (i.e. core stuff in objc or system in c#).
you might have to bite the bullet and just work your way through all of the pixels manually in o(n^2).
foreach horizontal pixel
foreach vertical pixel
if(pixel at (horizontal,vertical) is white)
return (horizontal, vertical)