`Theme` editor and programmatic methods to set Styles use completely different units - codenameone

This is my first attempt to develop a mobile application on Codename One. I am working on the presentation layer currently.
I notice there is an inconsistency between the Theme editor and the programmatic set and get methods of the available options.
E.g. in the editor, I set the colors in Hex since I use Material Design Color Tool Standards. When I try to reset a Component's color programmatically, I have the option to set it using only int RRGGBB argument.
Another example, I set the margin and padding units to millimeters in the Theme editor, but programmatically, there is no constant argument for millimeters option.
Please, if my assertions are wrong, leave a comment below to point my attention to it.

In response to your two observations:
colors in Hex and colors as Integer
Suppose you are interested in the "Light Blue 50" Material color, which hex value is: #E1F5FE. To express it as Integer, you should prefix the hex value with 0x, in this way: 0xE1F5FE. This is your Integer. See: https://www.tutorialspoint.com/hexadecimal-integer-literal-in-java
margin and padding units to millimeters
You must use the Style.UNIT_TYPE_DIPS to indicate millimeters, in this context they have the same meaning. I understand that you may be confused about that. In the javadoc (https://www.codenameone.com/javadoc/com/codename1/ui/plaf/Style.html#UNIT_TYPE_DIPS), you can read:
Indicates the unit type for padding/margin in device independent
pixels. Device independent pixels try to aim at roughly 1 millimeter
of the screen per DIP but make no guarantee for accuracy.
For example, to set a margin and a padding of half millimeter:
Form hi = new Form("Hi World", BoxLayout.y());
Button myBtn = new Button("My fun button 1", "Button-Red");
myBtn.getAllStyles().setMarginUnit(Style.UNIT_TYPE_DIPS);
myBtn.getAllStyles().setMargin(0.5f, 0.5f, 0.5f, 0.5f);
myBtn.getAllStyles().setPaddingUnit(Style.UNIT_TYPE_DIPS);
myBtn.getAllStyles().setPadding(0.5f, 0.5f, 0.5f, 0.5f);
hi.add(FlowLayout.encloseCenter(myBtn));
hi.show();

Related

Blending text, rendered by FreeType in color and alpha

I am using FreeType to render some texts.
The surface where I want to draw the text is a bitmap image with format ARGB, pre-multiplied alpha.
The needed color of the text is also ARGB.
The rendered FT_Bitmap has format FT_PIXEL_MODE_LCD - it is as the text is rendered with white color on black background, with sub-pixel antialiasing.
So, for every pixel I have 3 numbers:
Da, Dr, Dg, Db - destination pixel ARGB (the background image).
Fr, Fg, Fb - FreeType rendered pixel (FT_Bitmap rendered with FT_RENDER_MODE_LCD)
Ca, Cr, Cg, Cb - The color of the text I want to use.
So, the question: How to properly combine these 3 numbers in order to get the result bitmap pixel.
The theoretical answers are OK and even better than code samples.
Interpet the FreeType data not as actual RGB colors (these 'raw' values are to draw text in black) but as intensities of the destination text color.
So the full intensity of each F color component is F*C/255. However, since your C also includes an alpha component, the intensity is scaled by it:
s' = F*C*A/(255 * 255)
assuming, of course, that F, C, and A are inside the usual range of 0..255. A is a fraction A/255, and the second division is to bring F*C back into the target range. s' is now the derived source color.
On to plotting it. Per color component, the new color gets add to D, and D in turn gets dimished by the source's alpha 255-A (scaled).
That leads to the full sum
D' = D*(255-A)/255 + F*C*A/(255 * 255)
equal to (moving one value to the right)
D' = (D*(255-A) + F*C*A/255)/255
for each separate channel r,g,b of D, F, C and A. The last one, alpha, also needs a separate calculation for each channel because your FreeType output data returns this format.
If the calculation is too slow, you could compare the visual result with not-LCD-optimized grayscale output from FreeType. I suspect that especially on 'busy' (not entirely monochrome) backgrounds the extra calculations are simply not worth it.
The numerical advantage of a pure grayscale input is that you only have to calculate A and 1-A once for each triplet of RGB colors.
The "background" also has an alpha channel but to draw text "on" it you can regard this as 'unused'. Drawing a transparent item onto another transparent item does not, in general, change its intrinsic transparency.
After some discovery, I found the right answer. It is disappointing.
It is impossible to draw subpixel rendered graphics (including fonts) on a transparent image with RGBA format.
In order to properly render such graphics, a format that supports separate alpha channels for every color is mandatory.
For example 48 bit per pixes: RrGgBg where r, g and b are the alpha channels for the red, green and blue collor channels respectively.

What's the RGB Color For the Windows COLOR_WINDOW or COLOR_BACKGROUND?

In the Windows Api and GDI, you can use the default window background color for drawing buttons and stuff (that slight greyish color on Win98 , WinXP + Classic Theme etc. ).
What is the rgb value for that?
So I can emulate the exact color in Allegro using al_map_rgb( r, g, b) ?
It depends on the users settings.
You should use GetSysColor function to retrieve the DWORD value and then use GetRValue, GetGValue, and GetBValue to retrieve red, green and blue component values.
Although Bobrovsky's answer (use GetSysColor) is likely the right solution to the actual problem, if you want to know the default warm gray color regardless of the user's settings, it's R=212, G=208, B=200.
(The simple way to determine this was take a screenshot of a window, paste it into Paint, use the color picker tool on a bit of the default gray, and then open the "edit colors" dialog box to view the RGB values in it.)

Making a color completely transparent in OpenCV

I have a basic png file with two colors in it, green and magenta. What I'm looking to do is to take all the magenta pixels and make them transparent so that I can merge the image into another image.
An example would be if I have an image file of a 2D character on a magenta background. I would remove all the magenta in the background so that it's transparent. From there I would just take the image of the character and add it as a layer in another image so it looks like the character has been placed in an environment.
Thanks in advance.
That's the code i would use,
First, load your image :
IplImage *myImage;
myImage = cvLoadImage("/path/of/your/image.jpg");
Then use a mask like this to select the color, you should refer to the documentation. In the following, I want to select a blue (don't forget that in OpenCV images are in BGR format, therefore 125,0,0 is a blue (it corresponds to the lower bound) and 255,127,127 is blue with a certain tolerance and is the upper bound.
I chose lower and upper bound with a tolerance to take all the blue of your image, but you can select whatever you want...
cvInRangeS(image,
cvScalar(125.0, 0.0, 0.0),
cvScalar(255.0, 127.0, 127.0),
mask
);
Now we have selected the mask, let's inverse it (as we don't want to keep the mask, but to remove it)
cvNot(mask, mask);
And then copy your image with the mask,
IplImage *myImageWithTransparency; //You may need to initialize it before
cvCopy(myImage,myImageWithTransparency,mask);
Hope it could help,
Please refer to the OpenCVDocumentation for further information
Here it is
Julien,

CMYK to RGB formula of Photoshop

Is there a place where I can get the formula which photoshop uses to convert rgb to cmyk?
I know there are formulas on the web, but photoshop does not use this formula. It converts the collors different.
Can someone tell me the photoshop formula?
Thanks!
Most likely, Photoshop uses a color profile for converting RGB to CMYK.
If you want to do the same with a .NET language on Windows, then there's an API for it:
float[] colorValues = new float[4];
colorValues[0] = c / 255f;
colorValues[1] = m / 255f;
colorValues[2] = y / 255f;
colorValues[3] = k / 255f;
System.Windows.Media.Color color = Color.FromValues(colorValues,
new Uri(#"C:\Users\me\Documents\ISOcoated_v2_300_eci.icc"));
System.Drawing.Color rgbColor = System.Drawing.Color.FromArgb(color.R, color.G, color.B);
Note that two different Color classes from two different namespaces are used. And you probably need to add the PresentationCore DLL as a reference.
In this particular case, the ISO Coated v2 300% (ECI) profile is used. It can be downloaded from the downloads section of eci.org. It's part of a bigger ZIP file containing several profiles.
If you need to convert a complete image from CMYK to RGB, there are special classes in the same namespace that use a color profile as well.
There's a nice little online app for testing the CMYK color conversion with a color profile.
They are many different ICC Color Profiles for storing color as CMYK and RGB. There is no one end all be all encoding for color. There is RGB, sRGB, Adobe RGB, U.S. Web Coated (SWOP) v2, GRACol, etc.
As a print provider I'd ask what your end goal is. We can talk a bit about Photoshop scripting if you are looking to work with color objects in Adobe Javascript, but if this has to do with design I would lend this caution:
The RGB color space provides a wider color gamut (more colors), many photoshop effects are only available while working in RGB, and the RGB filesize is smaller (only storing 3 channels RGB, instead of 4 CMY & K).
If you save a document as CMYK when it goes to the printer device the printer hardware reinterprets the colors anyhow. So it does not benefit you to work in CMYK most of the time.
I do use PhotoShop, but a google search tells me that this varies by the devices you are using, and is controlled in Edit > Color Settings (Shift+Ctrl+K).

WPF VisualBrush with mixed aspect ratio

We are using the Mindfusion WPF Diagramming Component. I require a Visualbrush which must be able to be used in a third-party component for diagramming.
We have horizontally-stretching lanes on the diagramming canvas. We must define a background color for this lane, and some text on the top-left corner, to be repeated on the x-axis. The text must remain the same size while the rest of the lane is filled with color when the lane is re-sized.
Unfortunately, the only thing we can do is provide a Visualbrush to the diagramming component, which is then set as background for the lane. It seems impossible to work with this restriction because I am unable to define a text with fixed size, repeating on the x-axis, while also having a color brush stretching, all within a single Visualbrush. How can this be done?

Resources