Gtkmm getting label color - c++

I do not have a code error, I have just looked everywhere and cannot figure out how to do this. I want to get the color of a Gtk::widget, the Gtk::label. I can override the color of a label like this: l.override_color( c, l.get_state_flags() ); , but I have no idea how to get that color back from the label, thanks!

This is a way to set and get the color of a label:
// Set Color
Gtk::Label label("some label");
label.override_color (Gdk::RGBA("red"), Gtk::STATE_FLAG_NORMAL);
// Get Color
Glib::RefPtr<Gtk::StyleContext> stylecontext = label.get_style_context();
Gdk::RGBA color = stylecontext->get_color(Gtk::STATE_FLAG_NORMAL);
std::cout << color.to_string(); // Display color as "rgb(x, x, x)"

Related

QTextDocument and selection painting

I am painting a block of text on a widget with QTextDocument::drawContents. My current goal is to intercept mouse events and emulate text selection. It's pretty clear how to handle the mouse, but displaying the result puzzles me a lot.
Just before we start: I can not use QLabel and let it handle selection on it's own (it has no idea how to draw unusual characters and messes up line height (https://git.macaw.me/blue/squawk/issues/59)), nor I can not use QTextBrowser there - it's just a message bubble, I'm not ready to sacrifice performance there.
There is a very rich framework around QTextDocument, but I can not find any way to make it color the background of some fragment of text that I would consider selected. Found a way to make a frame around a text, found a way to draw under-over-lined text, but it looks like there is simply just no way this framework can draw a background behind text.
I have tried doing this, to see if I can take selected fragment under some QTextFrame and set it's style:
QTextDocument* bodyRenderer = new QTextDocument();
bodyRenderer->setHtml("some text");
bodyRenderer->setTextWidth(50);
painter->setBackgroundMode(Qt::BGMode::OpaqueMode); //this at least makes it color background under all text
QTextFrameFormat format = bodyRenderer->rootFrame()->frameFormat();
format.setBackground(option.palette.brush(QPalette::Active, QPalette::Highlight));
bodyRenderer->rootFrame()->setFrameFormat(format);
bodyRenderer->drawContents(painter);
Nothing of this works too:
QTextBlock b = bodyRenderer->begin();
QTextBlockFormat format = b.blockFormat();
format.setBackground(option.palette.brush(QPalette::Active, QPalette::Highlight));
format.setProperty(QTextFormat::BackgroundBrush, option.palette.brush(QPalette::Active, QPalette::Highlight));
QTextCursor cursor(bodyRenderer);
cursor.setBlockFormat(format);
b = bodyRenderer->begin();
while (b.isValid() > 0) {
QTextLayout* lay = b.layout();
QTextLayout::FormatRange range;
range.format = b.charFormat();
range.start = 0;
range.length = 2;
lay->draw(painter, option.rect.topLeft(), {range});
b = b.next();
}
Is there any way I can make this framework do a simple thing - draw a selection background behind some text? If not - is there a way I can unproject cursor position into coordinate translation, like can I do reverse operation from QAbstractTextDocumentLayout::hitTest just to understand where to draw that selection rectangle myself?
You can use QTextCursor to change the background of the selected text. You only need to select one character at a time to keep the formatting. Here is an example of highlighting in blue (the color of the text is highlighted in white for contrast):
void MainWindow::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.fillRect(contentsRect(), QBrush(QColor("white")));
QTextDocument document;
document.setHtml(QString("Hello <font size='20'>world</font> with Qt!"));
int selectionStart = 3;
int selectionEnd = selectionStart + 10;
QTextCursor cursor(&document);
cursor.setPosition(selectionStart);
while (cursor.position() < selectionEnd) {
cursor.movePosition(QTextCursor::Right, QTextCursor::KeepAnchor); // select one symbol
QTextCharFormat selectFormat = cursor.charFormat();
selectFormat.setBackground(Qt::blue);
selectFormat.setForeground(Qt::white);
cursor.setCharFormat(selectFormat); // set format for selection
cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1);
}
document.drawContents(&painter, contentsRect());
QMainWindow::paintEvent(event);
}

How to get the color of a pixel in an image in C++ (sfml)

I am working on a simple racing game where you drive a car on a track.
The track is grey and the background green, and any time the color i get on a given point (the front of the car) is not grey, the car should stop because it went out of the track.
However the track is NOT drawn with sfml, rather a downloaded image i made.
So, is there any method for getting the color of a pixel on an IMAGE, as long as the rgb values match?
Here is the pseudo code to do this:
while game is running
get color (car x value, car y value)
if color is not grey
car stops
Thank you!
You can get the sf::Color of a pixel in an sf::Image by using the appropriate method: sf::Image::getPixel. It takes X and Y coordinates.
Example:
sf::Image track;
if (!track.loadFromFile("track.jpg"))
{
// oops, loading failed, handle it
}
sf::Color color = track.getPixel(0, 0); // gets the color of the upper left corner pixel
You'll want to use sf::Image's getPixel(int x, int y) funciton. Your pseudo code would look something like this:
sf::Image track;
sf::Color grey_color; // You'll need to define what the grey color is.
// Take sf::Color(100, 100, 100) as an example.
if (!track.loadFromFile("track.jpg")) {
std::cout << "Uhoh" << std::endl;
}
while (gameIsRunning) {
sf::Color color_at_car = track.getPixel(car.getx(), car.gety());
if (color_at_car != grey_color) {
car.stop();
}
}
Hope this helps!

How to create Label with Chinese and Japanese characters with custom color

I use cocos2d-x (c++) ver. 3.8 on tvOS (Apple TV)
I don't want to use TTF versions because I don't have decent Arial TTF font which includes Chinese and Japanese characters and yet has acceptable file size (up to 1MB).
If I use the Label::createWithSystemFont() this doesn't allow to change the text color. At least I did not find it how to change it.
auto *n = Label::createWithSystemFont("洗牌位置", font, fontSize, dimensions, hAlignment, vAlignment);
n->setColor(Color3B::RED);
n->setTextColor(Color4B::RED);
This produces black text...
EDIT:
In a desperate situation I've tried something you might think I'm mad.
But it doesn't work neither. The result is still black. Would you know ANY way how to change the color of the Label??
Node* NodeFactory::colorizeLabel (Label *lbl, const Color3B& color)
{
auto *maskSprite = lbl;
CCRenderTexture * rt;
rt = CCRenderTexture::create(
maskSprite->getContentSize().width*maskSprite->getScaleX(),
maskSprite->getContentSize().height*maskSprite->getScaleY()
, kCCTexture2DPixelFormat_RGBA8888
);
maskSprite->setPosition(ccp(
(maskSprite->getContentSize().width*maskSprite->getScaleX())/2,
(maskSprite->getContentSize().height*maskSprite->getScaleY())/2
)
);
//rt->begin();
rt->beginWithClear(120, 120, 0, 0);
((Node*)maskSprite)->visit();
rt->end();
Sprite *retval = Sprite::createWithTexture(rt->getSprite()->getTexture());
retval->setColor(Color3B::WHITE);
//retval->setBlendFunc((ccBlendFunc) { GL_SRC_COLOR, GL_ONE });
return retval;
}
FOUND solution for colored system font on tvOS:
FIX: CCDevice-tvos.mm:
ORIG:
[str drawInRect:rect withAttributes:#{ NSFontAttributeName: font, NSParagraphStyleAttributeName:paragraphStyle }];
NEW:
[str drawInRect:rect withAttributes:#{ NSFontAttributeName: font, NSParagraphStyleAttributeName:paragraphStyle,
NSForegroundColorAttributeName:[UIColor colorWithRed:info->tintColorR green:info->tintColorG blue:info->tintColorB alpha:info->tintColorA]
}];

C++ pango text orientation

I'm trying to label the y-axis in a plot built using pango.
I'm unable to orient the text to run vertically up along the y-axis.
Relevant portion of code is:
#include <gtkmm.h>
Pango::FontDescription font;
cr->save(); // where cr is Glib::RefPtr<Cairo::Context> const &
cr->set_source_rgba(1.0,1.0,1.0,0.5);
font.set_family("Monospace");
font.set_style(Pango::STYLE_ITALIC);
font.set_weight(Pango::WEIGHT_BOLD);
font.set_absolute_size(20);
Glib::RefPtr<Pango::Layout> x_axis_label = this->create_pango_layout("x-axis label");
x_axis_label->set_font_description(font);
cr->move_to(0.38,0.465);
x_axis_label->show_in_cairo_context(cr);
// so far so good, renders as expected
// now trying to render the y-axis label
Glib::RefPtr<Pango::Context> t_y_axis_label_ctxt = x_axis_label->get_context();
Pango::Matrix p_matrix;
// apply some transformation
p_matrix.xx = 0.0;
p_matrix.xy = 1.0;
p_matrix.yx = 1.0;
p_matrix.yy = 0.0;
p_matrix.x0 = 0.0;
p_matrix.y0 = 0.0;
t_y_axis_label_ctxt->set_matrix(p_matrix);
Glib::RefPtr<Pango::Layout> y_axis_label = Pango::Layout::create(t_y_axis_label_ctxt);
y_axis_label->set_text("y-axis label"); // if this line of code is omitted I would expect, at least the text "x-axis label" to be rendered. But this does not happen.
y_axis_label->set_font_description(font);
cr->move_to(0.0,0.0);
y_axis_label->show_in_cairo_context(cr); // renders no output
cr->restore();
I suspect the problem has something to do with context that i retrieve from x-axis label, and the expected copy behaviour is not manifesting.
Are you sure you want to rotate the Pango context? I believe that functionality was introduced in the pre-Cairo days; now you would just rotate the Cairo context, like:
cr->save();
cr->rotate_degrees(90);
Glib::RefPtr<Pango::Layout> y_axis_label = this->create_pango_layout("y-axis label");
y_axis_label->set_font_description(font);
cr->move_to(...); // wherever you wanna put it
y_axis_label->show_in_cairo_context(cr);
cr->restore();
If you still don't see anything, make sure you are not moving the label out of the visible area.

JFreechart LookUpPaintScale color gradient

How do you add a color gradient (3 colors: red, yellow, Green) to a LookUpPaintScale in JFreechart?
Thanks
I tried initializing it with the new GradientPaint, but this only take 2 colors.
You could try LinearGradientPaint.
you can try something like this
....
XYPlot plot = new XYPlot(data1, xAxis, yAxis, null);
LookupPaintScale ps = new LookupPaintScale(0,101,Color.lightGray);
ps.add(0, Color.green);
ps.add(10, Color.yellow);
ps.add(20, Color.red);
renderer1.setBlockHeight(0.95f);
renderer1.setBlockWidth(0.95f);
renderer1.setPaintScale(ps);
plot.setRenderer(renderer1);
....