How to change the bar color of FL_HOR_NICE_SLIDER in fltk? - fltk

I want to change the bar color of FL_HOR_NICE_SLIDER to green, so I tried the following code:
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Slider.H>
int main(int argc, char **argv) {
Fl::scheme("GTK+");
Fl::background(50, 50, 50);
Fl::background2(90, 90, 90);
Fl::foreground(255, 255, 255);
Fl_Window *window = new Fl_Window(400, 60);
Fl_Slider *slider = new Fl_Slider(20, 20, 300, 20);
slider->type(FL_HOR_NICE_SLIDER);
slider->box(FL_FLAT_BOX);
slider->color(0x00DD0000);
slider->color2(0xDDDDDD00);
window->end();
window->show(argc, argv);
return Fl::run();
}
The result of this code is shown below. The bar color remains white, but the "area" color changes to green, which is not my desired result.
What I would like to achieve is the following result:
(I use Fl::foreground(0, 255, 0); and delete slider->color(0x00DD0000); to get the result above, but I don't want to change the foreground color because this will change other colors as well, for example the default font color).
How can I achieve the expected result?

After checking out the source code of fltk, I disappointedly discovered that I can only change the bar color by changing the foreground color.
The source code of drawing the bar of FL_HOR_NICE_SLIDER is shown below:
void Fl_Slider::draw_bg(int X, int Y, int W, int H) {
fl_push_clip(X, Y, W, H);
draw_box();
fl_pop_clip();
Fl_Color black = active_r() ? FL_FOREGROUND_COLOR : FL_INACTIVE_COLOR;
if (type() == FL_VERT_NICE_SLIDER) {
draw_box(FL_THIN_DOWN_BOX, X+W/2-2, Y, 4, H, black);
} else if (type() == FL_HOR_NICE_SLIDER) {
draw_box(FL_THIN_DOWN_BOX, X, Y+H/2-2, W, 4, black);
}
}
Note that the color is either FL_FOREGROUND_COLOR or FL_INACTIVE_COLOR, depending on whether the slider is active or not.

Related

GLSL color with texture ontop

Is it possible to get GLSL to produce this:
This is my fragment shader:
#version 120
uniform sampler2D diffuse;
varying vec3 shared_colors;
varying vec2 shared_texCoords;
void main() {
vec4 color = vec4(shared_colors, 1);
vec4 texture = texture2D(diffuse, shared_texCoords);
vec4 finalColor = vec4(mix(color.rgb, texture.rgb, texture.a), 1);
vec4 fCol = color * texture;
gl_FragColor = fCol;
}
My results are:
finalColor = red color, no texture
fCol = no color (black), red texture
I want to set the color of the object and have that show through wherever the alpha of the texture is less than 1...
Apparently, my texture was not loaded correctly after all, as it gave me a constant alpha value of 0, so when I tried to call the Mix() in the GLSL it ended up canceling it completly out.
I loaded the texture like this:
SDL_CreateRGBSurface(NULL, rawImage->w, rawImage->h, 32, 0, 0, 0, 0);
It was solved by setting it to this, so it actually conciders the alpha channel correctly:
SDL_CreateRGBSurface(NULL, rawImage->w, rawImage->h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
Also after that I found out my texture was loaded with reversed rgb channels (meaning it used bgr), so I also changed that when merging the texture in the shader.
From:
vec4 finalColor = vec4(mix(color.rgb, texture.rgb, texture.a), 1);
To:
vec4 finalColor = vec4(mix(color.rgb, texture.bgr, texture.a), 1);

DrawText doesn't work, but Graphics::DrawString is ok

I am creating a bitmap in the memory which combine with an image and text. My code is:
HDC hdcWindow = GetDC();
HDC hdcMemDC = CreateCompatibleDC(hdcWindow);
HBITMAP hbmDrag = NULL;
if (!hdcMemDC) {
ReleaseDC(hdcWindow);
return NULL;
}
RECT clientRect = {0};
GetClientRect(&clientRect);
hbmDrag = CreateCompatibleBitmap(hdcWindow, 256, 256);
if(hbmDrag) {
SelectObject(hdcMemDC, hbmDrag);
FillRect(hdcMemDC, &clientRect, mSelectedBkgndBrush);
Graphics graphics(hdcMemDC);
// Draw the icon
graphics.DrawImage(mImage, 100, 100, 50, 50);
#if 1
CRect desktopLabelRect(0, y, clientRect.right, y);
HFONT desktopFont = mNameLabel.GetFont();
HGDIOBJ oldFont = SelectObject(hdcMemDC, desktopFont);
SetTextColor(hdcMemDC, RGB(255,0,0));
DrawText(hdcMemDC, mName, -1, desktopLabelRect, DT_CENTER | DT_END_ELLIPSIS | DT_CALCRECT);
#else
// Set font
Font font(hdcMemDC, mNameLabel.GetFont());
// Set RECT
int y = DEFAULT_ICON_HEIGHT + mMargin;
RectF layoutRect(0, y, clientRect.right, y);
// Set display format
StringFormat format;
format.SetAlignment(StringAlignmentCenter);
// Set brush
SolidBrush blackBrush(Color(255, 0, 0, 0));
// Draw the label
int labelWide = DEFAULT_ICON_WIDTH + mMargin;
CString labelName = GetLayOutLabelName(hdcMemDC, labelWide, mName);
graphics.DrawString(labelName, -1, &font, layoutRect, &format, &blackBrush);
#endif
}
DeleteDC(hdcMemDC);
ReleaseDC(hdcWindow);
return hbmDrag;
The image can be outputted to the bitmap success.
For the text, if I use "DrawText", it can't be shown in the bitmap although the return value is correct;
But Graphics::DrawString can output the text success.
I don't know the reason. Anybody can pls tell me?
Thanks a lot.
You are passing the DT_CALCRECT flag to DrawText(). This flag is documented as (emphasis mine):
Determines the width and height of the rectangle. If there are
multiple lines of text, DrawText uses the width of the rectangle
pointed to by the lpRect parameter and extends the base of the
rectangle to bound the last line of text. If the largest word is wider
than the rectangle, the width is expanded. If the text is less than
the width of the rectangle, the width is reduced. If there is only one
line of text, DrawText modifies the right side of the rectangle so
that it bounds the last character in the line. In either case,
DrawText returns the height of the formatted text but does not draw
the text.

How to Change Background Color on Processing?

I'm still extremely new to processing and I'm just playing around with it right now. I was hoping to find out how to change the background color between two colors, particularly between white and black, when I click my mouse. I found a code online that has the background color change between several different colors but I can't seem to figure out how the bg color can be changed between two colors. I would particularly like what 'col+=' and 'col%=' represent because I can't seem to find it in the processing tutorial. Please help me! Thank you!
Below is the code that I found.
void setup() {
size(600,400);
smooth();
colorMode(HSB);
}
int col = 0;
void draw() {
background(col,255,255);
}
void mousePressed(){
col+=20;
col%=255;
println(col);
}
"x += y" is shorthand for "x = x + y" and, likewise, "x %=y" is shorthand for "x = x % y" (where % is the modulo operator).
I'm going to assume that you wanted to ask is "how do I change the background from one colour to another, and then back again"; there's two basic ways to do this.
1: set up two (or more) reference colours, an extra "current" colour, and then change what 'current' points to, drawing the background off of that:
color c1 = color(255,0,0), c2 = color(0,0,255), current;
void setup() { current = c1; }
void draw() { background(current); }
void mousePressed() { if(current==c1) { current = c2; } else { current = c1; }}
Every time you click, the program checks which of the two colours "current" points to, and then instead points it to the other colour.
2: set up one colour, and apply some operation that is modulo in 1, or 2, or ... steps:
color c = color(255,0,0);
void draw() { background(c); }
void mousePressed() { c = color( red(c), (green(c)+50)%255, blue(c)); }
Every time you click, the colour "c" gets its green component increased by 50, and then modulo-corrected for 255. So it'll cycle through: 0, 50, 100, 150, 200, 250, 300%255=45, 95, 145, 195, 245, 295%255=40, 90, etc.

How to set OpenGl Coordinates?

With following code I am getting my triangle in top right corner of the graph, which tells me that the 0,0 is in the center of the window. What should I do to bring it in the corner of the window, i.e. bottom left?
#include <GL/glut.h>
void displayCube()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glBegin(GL_TRIANGLES);
glVertex3f(0, 0, 0);
glVertex3f(0.5, 0, 0);
glVertex3f(0.25, 0.25, 0);
glEnd();
glFlush();
}
int main(int argc, char *argv[]){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Cube");
glutDisplayFunc(displayCube);
glutMainLoop();
return 0;
}
OpenGL uses a set of matrix transformation to move from original model space to screen/window space.
In you example, there is default identity projection so you are 'moving' in box -1 to 1 in each direction.
point (0.0, 0.0, 0.0) is in the centre. (-1, 0, 0) is on the left side, (1, 0, 0) is on the right, (0, 1, 0) is top.
try to figure out the rest :)
http://www.songho.ca/opengl/gl_transform.html

Qt + XCompositeRender problem

I want to render the contents of the window in to QWidget ( or QPixmap ) using XComposite and XRender.
The Issue I'm facing is that I can't get the picture be rendered in the QWidget. The code below has been written using the following tutorial: http://ktown.kde.org/~fredrik/composite_howto.html
The window ID is hardcoded, so there can be any other window ID used. QWidget window which opens does not display the contents of the original window, but just shows the blank gray rectangle. The same thing is if I use the QPixmap, it contains just black rectangle and nothing else.
XRender support is enabled.
What I'm missing here ?
int main( int argc, char *argv[] )
{
QApplication app( argc, argv );
Display *dpy = XOpenDisplay( getenv("DISPLAY") );
Window window = 2097154;
XCompositeRedirectWindow( dpy, window, CompositeRedirectManual );
XWindowAttributes attr;
XGetWindowAttributes( dpy, window, &attr );
XRenderPictFormat *format = XRenderFindVisualFormat( dpy, attr.visual );
bool hasAlpha = ( format->type == PictTypeDirect && format->direct.alphaMask );
int x = attr.x;
int y = attr.y;
int width = attr.width;
int height = attr.height;
qDebug() << hasAlpha << x << y << width << height;
XRenderPictureAttributes pa;
pa.subwindow_mode = IncludeInferiors; // Don't clip child widgets
QWidget widget;
widget.setGeometry( 100, 100, 500, 500 );
widget.show();
Picture picture = XRenderCreatePicture( dpy, window, format, CPSubwindowMode, &pa );
XRenderComposite( dpy, PictOpSrc, picture, None,
widget.x11PictureHandle(), 0, 0, 0, 0, 0, 0, 500, 500 );
XRenderFreePicture( dpy, picture );
return app.exec();
}
Looks like you need to use
app.setGraphicsSystem("native");
instead of default raster graphic system to have QPixmap buffer be stored.

Resources