GDI+ resets color of transparent part of image to black - gdi+

Ho do I draw a specific transparent color with GDI+ ?
I tried this code:
m_image = new Gdiplus::Bitmap( img_w, img_h );
m_graphic = Gdiplus::Graphics::FromImage( m_image );
Gdiplus::Color c( 0, 255, 0, 0 ); // ARGB = 0x00FF0000
m_graphic->Clear( c );
m_image->GetPixel( 0, 0, &c ); //ARGB = 0x00000000 ?!
The color of transparent part of the image is always black. How can I change this?

The Graphics::Clear method clears a Graphicsobject to a specified color.
I have tried your code:
Image m_image(L"C:\\Users\\strives\\Desktop\\apple.jpg");
Graphics *m_graphic = Graphics::FromImage(&m_image);
Gdiplus::Color c(0, 255, 0, 0); // ARGB = 0x00FF0000
m_graphic->Clear(c);
graphics.DrawImage(&m_image, 30, 20);
delete m_graphic;
The final picture is like this.
I think the problem is clear. If you use the clear function and set the color to (0, 255, 0, 0), which defaults to black, then the printed area must be black, and the color of the pixels captured by the GetPixel function below you must be black.

Related

state Warning Validation (CSS 4.0): "rgb(0, 0, 0, 0.4)" is not a valid value for the "border" property

I am finishing up my asp core 3 project and fixing some warning that occurred,but i cannot find the reason for this one?the css property works completely fine adding some transparency to the black border.What causes the warning?
PS:My IDE is Visual Studio 2019
RGBa (Red = 0, Green = 0, Blue = 0, Alpha = 0.4)
it's different from rgb(0, 0, 0)
for border short hand:
border: 1px solid rgba(0, 0, 0, 0.4)
The rgb() syntax expects only three values. To include the fourth alpha value, you should use rgba() instead.

Interpret box-shadow as canvas shadowXXX options

I have to draw a bow object in a canvas (2d). The box has an external shadow specified as css definition
box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.15)
I don't know how to convert this to the canvas way of defining shadows, using shadowOffsetX/Y, shadowColor and shadowBlur.
If I look at the shadowBlur spec, it's explicitely not related to pixels, but it only says "it's a parameter for a gaussian blur effect" (paraphrasing). Actually, I find this to be under-specified.
Would a better approximation using a gradient to transparent instead ? But then won't it miss the blurring effect ?
Now there are filters which are similar to the CSS filters properties and accepts the same functions. Although it is still a experimental technology.
// create canvas
const canvas = document.createElement("canvas");
canvas.width = 500;
canvas.height = 500;
// set context
const context = canvas.getContext("2d");
context.fillStyle = '#ff0';
context.rect(50, 50, 100, 100);
// set shadow filter
let offsetX = 20;
let offsetY = 0;
let blurRadius = 5;
let color = 'rgba(255, 0, 0, 0.8)';
context.filter = 'drop-shadow('+ offsetX + 'px ' + offsetY + 'px ' + blurRadius + 'px ' + color + ')';
context.fill();
document.body.appendChild(canvas);
The shadow blur is in pixels, and only supports pixel dimensions. A blur of 0 has a sharp edge, 1 is a blur of one pixel, 2 two pixels and so on. It is not affected by the 2d API transformation. Shadow spread is not supported by the canvas 2D API.
BTW values should be qualified. I have added px where you forgot them in the CSS.
So CSS
box-shadow:0px 1px 2px 0px rgba(0, 0, 0, 0.15);
becomes
ctx.shadowBlur = 2;
ctx.shadowColor = "rgba(0, 0, 0, 0.15)";
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 1;

QTreeWidget Stylesheet Color for Childs

System: Linux Mint, QT Creator from Repo -> QT Version 5.2, C++)
I've created a Customwidget wich Im using inside a QTreeView
OwnItem *OI = new OwnItem;
QTreeWidgetItem *itemN = new QTreeWidgetItem();
ui->ProjektListe->addTopLevelItem(itemN);
ui->ProjektListe->setItemWidget(itemN, 0, OI);
What I want is to set up a Stylesheet for the QTreeWidget including a Backgroundcolor and a Textcolor in Normal Mode and Selected Mode.
So far:
QTreeWidget::item{
background-color: rgb(255, 255, 255);
color: rgb(255, 255, 0);
}
QTreeWidget::item:selected{
background-color: #157efb;
color: rgb(255, 0, 0);
}
The Problem is that The Backgroundcolor works, the Color (TextColor) not (in both Cases). Im aware that when stylesheets for childs are set separately this won't work but the widget itself and all of its children (Some Labels and Buttons) are "Sylesheet" free.
The only Case "color: .... " for TextColor works is this case
QWidget{
color: rgb(85, 0, 0);
}
But this wont work with the "selected" status
My anser is in C++ not for the CSS but you can create a QPalette then set the values that you want to with the function void QPalette::setColor ( ColorGroup group, ColorRole role, const QColor & color ) so for you it should me something like:
QTreeWidget tree(a);
QPalette palette;
palette.setColor(QPalette::Window, QColor(255, 255, 255));
palette.setColor(QPalette::WindowText, QColor(255, 255, 0));
palette.setColor(QPalette::Highlight, QColor(255, 0, 0))
palette.setColor(QPalette::HighlightedText, QColor(0, 0, 255));
QList<QTreeWidgetItem> treeItems = tree.findChildren<QTreeWidgetItem*>();
foreach (QTreeWidgetItem *w : treeItems) {
w.setPalette(palette);
}
the findChildren will return a list with all the children to the widget then you can set the palette. To find the list of the colors groups you can go here: http://qt-project.org/doc/qt-4.8/qpalette.html#setColor then click on the ColorGroup type in the parameter then you will be are here: http://qt-project.org/doc/qt-4.8/qpalette.html#ColorGroup-enum
Good luck !

drawing text in PlayN doesn't appear

I'm trying to draw a rectangle with text on it, but all I see is the rectangle, there is no text. Am I doing something wrong?
This is the part of the code that does the drawing:
CanvasImage image = PlayN.graphics().createImage(100, 50);
Canvas canvas = image.canvas();
canvas.setFillColor(color);
canvas.fillRect(0, 0, 100, 50);
canvas.setFillColor(textColor);
canvas.setStrokeColor(textColor);
canvas.drawText("test", 0, 0);
layer.surface().drawImage(image, 0, 0);
Thanks
btw, I'm running the HTML version.
The problem was that the text is drawen up from the y coordinate and I was setting y to 0 so the text wasn't shown.

When using AlivePDF library in Flex the beginFill method sets font colour rather than background colour

Code sample:
var headerRowBackground:RGBColor = new RGBColor(0);
headerRowBackground.b = 58;
headerRowBackground.g = 28;
headerRowBackground.r = 255;
printPDF.beginFill(headerRowBackground);
printPDF.addCell(30, 20, "Room");
The word "Room" is in red, as is the rest of the text in the PDF. I actually want to make the cell background colour red. Anybody know why this doesn't work?
You should look at the API more:
printPDF.addCell(30, 20, 'Room', 0, 0, '1', 0xFF0000);
The documentation is wrong, the fill parameter is described as "Link can be internal to do document level navigation (InternalLink) or external (HTTPLink)".
The code to get this working is:
printPDF.beginFill(new RGBColor(0xFF0718));
printPDF.textStyle(new RGBColor(0x000000));
printPDF.addCell(30, 10, "Room", 0, 0, Align.LEFT, 1);
A couple of things about the code:
The fill parameter should be 0 or 1
rather than the fill value. It just
either switches on or off the fill
value previously set.
The text style
should be set too otherwise the text
and background will use the same
colour

Resources