Accessing css custom theme constant from code - codenameone

I wonder whether it's possible to retrieve custom constant values set in the css file from the java code.
I went through the UIManager.themeConstants (as well as themeProps, you never know :)) but I could not find my custom constants in there.
I tried the following:
#Constants {
--color0: #C4DFE6;
}
int color = UIManager.getInstance().getThemeConstant("--color0", 0);
System.out.println("COLOR0=" + color);
color = UIManager.getInstance().getThemeConstant("color0", 1);
System.out.println("COLOR0=" + color);
color = UIManager.getInstance().getThemeConstant("var(--color0)", 2);
System.out.println("COLOR0=" + color);
color = UIManager.getInstance().getThemeConstant("var(color0)", 3);
System.out.println("COLOR0=" + color);
I was hoping one of them to return my value: 0xC4DFE6.

The -- syntax is a special case for use within the CSS. Try using MyColor in the CSS with the same syntax in the Java side and it should work.

Related

How to define the color of a GtkButton using Gtk+3 (in C)

I'm using GTK+3 (with the C language) to create a Battleship game, using an array of buttons, but I have some issues related to the color of them. I've already changed the background color of my array, and, now, I want to change the color of the buttons itself. I'm trying to use the gtk_widget_override_color() function to do that, but appears that it doesn't work in my code.
Could you take a look and maybe suggest other functions, which can work?
The code that I'm using is down here:
for(i=0;i<(x[0].n*x[0].n);i++){
gtk_widget_modify_bg(GTK_WIDGET(buttons[i]),GTK_STATE_NORMAL,&color);
gtk_widget_override_color(GTK_WIDGET(buttons[i]),GTK_STATE_NORMAL,&color2);
}
Color variable is of GdkColor type, and it has been defined with gdk_color_parse(), while color2 is of the type struct GdkRGBA, and it has been defined with the following lines:
color2.alpha = 0.8;
color2.blue = 0.819;
color2.red = 0;
color2.green = 0.807;
You should be using CSS for this. Something like:
GtkCssProvider *provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (provider,
"button { color: #123456; background-color: blue; }", -1, &error);
gtk_style_context_add_provider_for_screen (gdk_screen_get_default (),
provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
This will give all your buttons the supplied foreground and background colors.

Workaround for applying a mask to an image in Codename One

I have a function that accepts an image and rounds it, like so:
public static Image roundImage(Image img) {
int width = img.getWidth();
Image roundMask = Image.createImage(img.getWidth(), img.getHeight(), 0xff000000);
Graphics gr = roundMask.getGraphics();
gr.setColor(0xffffff);
gr.fillArc(0, 0, width, width, 0, 360);
Object mask = roundMask.createMask();
img = img.applyMask(mask);
return img;
}
This works great for images, but if I pass FontImage to it, the function throws an exception: java.lang.RuntimeException: Unsupported Operation. How can I check whether masking is a supported operation? I want to avoid changing the application logic.
FontImage is a requiresDrawImage which is unique. You can convert it to a regular image or even to an encoded image using toImage() or toEncodedImage() respectively.
For this use case a regular image will be better/faster so toImage() should work best.

Font layouting & rendering with cairo and freetype

I have a system that has only the freetype2 and cairo libraries available. What I want to achieve is:
getting the glyphs for a UTF-8 text
layouting the text, storing position information (by myself)
getting cairo paths for each glyph for rendering
Unfortunately the documentation doesn't really explain how it should be done, as they expect one to use a higher level library like Pango.
What I think could be right is: Create a scaled font with cairo_scaled_font_create and then retrieve the glyphs for the text using cairo_scaled_font_text_to_glyphs. cairo_glyph_extents then gives the extents for each glyph. But how can I then get things like kerning and the advance? Also, how can I then get paths for each font?
Are there some more resources on this topic? Are these functions the expected way to go?
Okay, so I found what's needed.
You first need to create a cairo_scaled_font_t which represents a font in a specific size. To do so, one can simply use cairo_get_scaled_font after setting a font, it creates a scaled font for the current settings in the context.
Next, you convert the input text using cairo_scaled_font_text_to_glyphs, this gives an array of glyphs and also clusters as output. The cluster mappings represent which part of the UTF-8 string belong to the corresponding glyphs in the glyph array.
To get the extents of glyphs, cairo_scaled_font_glyph_extents is used. It gives dimensions, advances and bearings of each glyph/set of glyphs.
Finally, the paths for glyphs can be put in the context using cairo_glyph_path. These paths can then be drawn as wished.
The following example converts an input string to glyphs, retrieves their extents and renders them:
const char* text = "Hello world";
int fontSize = 14;
cairo_font_face_t* fontFace = ...;
// get the scaled font object
cairo_set_font_face(cr, fontFace);
cairo_set_font_size(cr, fontSize);
auto scaled_face = cairo_get_scaled_font(cr);
// get glyphs for the text
cairo_glyph_t* glyphs = NULL;
int glyph_count;
cairo_text_cluster_t* clusters = NULL;
int cluster_count;
cairo_text_cluster_flags_t clusterflags;
auto stat = cairo_scaled_font_text_to_glyphs(scaled_face, 0, 0, text, strlen(text), &glyphs, &glyph_count, &clusters, &cluster_count,
&clusterflags);
// check if conversion was successful
if (stat == CAIRO_STATUS_SUCCESS) {
// text paints on bottom line
cairo_translate(cr, 0, fontSize);
// draw each cluster
int glyph_index = 0;
int byte_index = 0;
for (int i = 0; i < cluster_count; i++) {
cairo_text_cluster_t* cluster = &clusters[i];
cairo_glyph_t* clusterglyphs = &glyphs[glyph_index];
// get extents for the glyphs in the cluster
cairo_text_extents_t extents;
cairo_scaled_font_glyph_extents(scaled_face, clusterglyphs, cluster->num_glyphs, &extents);
// ... for later use
// put paths for current cluster to context
cairo_glyph_path(cr, clusterglyphs, cluster->num_glyphs);
// draw black text with green stroke
cairo_set_source_rgba(cr, 0.2, 0.2, 0.2, 1.0);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, 0, 1, 0, 1.0);
cairo_set_line_width(cr, 0.5);
cairo_stroke(cr);
// glyph/byte position
glyph_index += cluster->num_glyphs;
byte_index += cluster->num_bytes;
}
}
Those functions seem to be the best way, considering Cairo's text system. It just shows even more that Cairo isn't really meant for text. It won't be able to do kerning or paths really. Pango, I believe, would have its own complex code for doing those things.
For best advancement of Ghost, I would recommend porting Pango, since you (or someone else) will probably eventually want it anyway.

How to Change Color of a cell in excel from Silverlight using Com AutomationFactory

Hi
Im using AutomationFactory from silverlight to create and manipulate Exel worksheet.
And I want to change Color of a cell. If I understand correctly I must change this property
cell.Interior.Color =
However I want to change it to MyObject.Color (whitch is of tzpe Color)
MSDN Says i should use RGB function to assign cell.Interior.Color
But there is no RGB function in silverlight!?
How to change Color to something cell.Interior.Color will understand?
I made RGB function..
internal static int RGB(Color color)
{
string hexaString=color.B.ToString("X2") + color.G.ToString("X2") + color.R.ToString("X2");
int rgb = int.Parse(hexaString, System.Globalization.NumberStyles.HexNumber);
return rgb;
}

Proper color names from colordialog

Whenever I run this, and open the color dialog, there are many colors that do not having a proper name, the listbox will show something like "ffff8000"(Orange-Yellow). Is there another way of pushing the proper name? Is there a proper Color Name library I can reference in code?
colorDialog1.ShowDialog();
cl.Add(colorDialog1.Color.Name);
listBox1.Items.AddRange(cl.ToArray());
The .NET framework defines the KnownColor enum, you could use it to convert a color value to a name. It won't be a complete solution, it doesn't have "Orange Yellow". But many of the common colors are present. For example:
public static Color LookupKnownColor(uint c) {
int crgb = (int)(c & 0xffffff);
foreach (KnownColor kc in Enum.GetValues(typeof(KnownColor))) {
Color map = Color.FromKnownColor(kc);
if (!map.IsSystemColor) {
if ((map.ToArgb() & 0xffffff) == crgb)
return map;
}
}
return Color.FromArgb(unchecked((int)(c | 0xff000000)));
}
Usage:
Color c = LookupKnownColor(0xffffff00);
Console.WriteLine(c.Name);
Output: Yellow

Resources