Preferable way to set background and font dynamically in JavaFX - css

I have to update the nodes styling dynamically based on my data. Mostly it will be background, border and font. I cannot define predefined css class names to define background color as the colors will be dynamic. Same as with border and fonts.
I am aware that I can set background to a Region in two ways. By constructing the style string and set in setStyle method
region.setStyle("-fx-background-color:"+getColorStr()+";");
or alternatively I can construct a Background object and set using setBackground.
Background background = new Background(new BackgroundFill(getColor(), new CornerRadii(0), Insets.EMPTY));
region.setBackground(background);
I am a bit confused to choose which option to go with. Mainly worried about the performance issue of one option over other. Can someone let me know which is most preferrable way to go with (considering performance) ?
Just to let you know my CSS file is a heavy file with complete customisation of all controls. Can this be a factor to consider for performance if I go with setStyle method.
The same question when dealing with Border and Font as well.

Related

How can I use a font icon as a background image?

1) I know I can use a font icon using , pseudo selectors (before, after) and "content", but it is not a scalable solution, because I need to make many changes to padding, left, top etc in different situations
2) Using Data URI an svg is ok, but I need to change the fill/color of the svg
based on different actions. I tried the fill property but it is not really working.
I can change/edit the color external but I have a lot of variations and again doesn't scale
The icon fonts(ex: FontAwesome) come with SVG files for the browsers. It is possible to use somehow that file in css (like image sprites) ?
If you want to modify properties of SVG's it will have to be placed inline. So using an SVG as a background and modifying properties won't work.
Libraries like FontAwesome give you a font, not svg's which you can't set as a background either.
I don't see the problem of using the psuedo-selectors :before and :after for this. They are very scalable in my opinion.
If you really want to do it differently you should share some of your code with us so we can get a better understanding of what you're trying to do.

Is there a way to use CSS to specify colors on an HTML5 canvas?

I'm collaborating on a checkers program for which the first logical step is to draw the initial board. The implementation we are using has one philosophical issue. The squared are formed by calling fillRect with the color of the"dark" squares after initializing the board with the color of the light squares.
My concern is that I now have colors hard coded into the JS, and I would prefer to specify them in the associated CSS. Is there a good way to encode the colors used in drawing the canvas in CSS?
Pick one or the other, and stick to it. Obviously the game logic would still use JS.
If you want to make it easy to style each part of the board, use CSS and render the parts of the board using actual HTML elements with classes on them. This way you can easily make changes to sizes, colours, images, textures, etc., without explicitly re-rendering anything.
If you want/need it to be on a canvas, you can easily expose a method for setting styling options during or after game creation. This way you can include an inline or second script file as you would with CSS.

How to set QPalette ColorRole properties with stylesheets

I am using a pretty lightweight custom palette that just defines a few colors and properties. I am attempting to transform it into a stylesheet but I am running into problems. For example, what is the QPalette::Base equivalent in stylesheets? Do I have to change the background color for every widget individually?
I tried setting a few stylesheet properties for the QWidget but then I got undesired results for widgets with borders. Layouts were showing borders when I didn't want them to, so I found myself writing a lot of stylesheet definitions to add/remove borders.
The gist of the question is, is there a good way to convert a palette to a stylesheet?

Dynamically change background-color

I want to change the background color of my Vaadin app on the valueChange event of a OptionGroup component.
As there are a fairly big number of layouts on my page, what would be the best is to change every HorizontalLayout and VerticalLayout background-color, but every new component that could be added should have this color too.
How can I achieve that? Thanks.
Two approaches come to mind.
The simplest, conceptually, as well as in terms of implementation, would be to have a single component to serve as the background, and give your HorizontalLayout and VerticalLayout components a transparent background. If you're layout is too complex for this, you can have a number of base 'background' components, again, with your layout components on top being transparent. The idea is to reduce the components whose color you need to change to something manageable.
If that approach doesn't translate to your use case, you can always use JavaScript to select the layout components on the fly and redefine their background. This could be made more manageable by giving all the layout components a common CSS class (eg: 'dynamic-background').
JavaScript.getCurrent().execute("$('.dynamic-background').css('background-color', 'purple')");
Note that this JavaScript depends on JQuery, have a look around for more details on integrating JQuery with Vaadin:
Add javascript/Jquery & client side code in Vaadin 7
Integrating HTML and JavaScript in Vaadin 7

Drawbacks to using background-repeat only for colors?

So I need some custom colors on a layout, but I'm looking for a better way of doing it other than just slapping a giant picture with (background: url(something.jpg)) in the layout.
Mostly I'm thinking of getting a color palette (i.e. from Adobe Kuler, colourlovers, etc.), getting a 5x5 sample of each color and sticking them in an array for CSS sprites or just as separate files and accessing them through: .color-one {transparent url(./one.gif) repeat} and just reusing that whenever I'd like to use the color.
Are there any drawbacks to doing it this way? And if there are should I just stick with web-safe colors or is there a better way of doing this?
You don't need graphics to represent background colors. You are going the long way around if you use images for that. Just use colors, as graphicsdivine suggests.
Only use background images if you need to do gradients and the like. That's really where they shine.
As to your second question, no, you don't need to stick with "web-safe" colors anymore. If someone in 2010 still only can display 256 colors, well, your site won't be their biggest problem.
.color-one{background-color: #f00}
.color-two{background-color: #0f0}
You have to set the colors as background-color anyway to serve readers which don't load images. So I see no use in those images.
And remember: the smaller an image is the more has the browser to compute to calculate all positions. Repeated background images should not be smaller than 20×20px.
Why don't set background-color?
(And your suggestion wouldn't work with sprites, the renderer will also use the other parts of the image)

Resources