FLTK logo

[master] 699cca8 - Make Fl::set_color(r,g,b,a) effective under Wayland and macOS.

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] 699cca8 - Make Fl::set_color(r,g,b,a) effective under Wayland and macOS. "ManoloFLTK" Sep 15, 2022  
 
commit 699cca8ff4367c15ea0cda1b5750c68eac174816
Author:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
AuthorDate: Thu Sep 15 15:01:12 2022 +0200
Commit:     ManoloFLTK <41016272+ManoloFLTK@users.noreply.github.com>
CommitDate: Thu Sep 15 15:01:12 2022 +0200

    Make Fl::set_color(r,g,b,a) effective under Wayland and macOS.

 src/Fl_Graphics_Driver.cxx                         |  1 +
 src/Fl_cocoa.mm                                    |  2 ++
 src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx     | 27 +++++++++++++++++++---
 .../Quartz/Fl_Quartz_Graphics_Driver_color.cxx     |  9 ++++++--
 src/fl_color.cxx                                   |  2 ++
 5 files changed, 36 insertions(+), 5 deletions(-)

diff --git src/Fl_Graphics_Driver.cxx src/Fl_Graphics_Driver.cxx
index 159a76b..3bec7c2 100644
--- src/Fl_Graphics_Driver.cxx
+++ src/Fl_Graphics_Driver.cxx
@@ -45,6 +45,7 @@ Fl_Graphics_Driver::Fl_Graphics_Driver()
 {
   font_ = 0;
   size_ = 0;
+  color_ = FL_BLACK;
   sptr=0; rstackptr=0;
   rstack[0] = NULL;
   fl_clip_state_number=0;
diff --git src/Fl_cocoa.mm src/Fl_cocoa.mm
index ddcb6d4..89636fd 100644
--- src/Fl_cocoa.mm
+++ src/Fl_cocoa.mm
@@ -2239,6 +2239,8 @@ static FLTextInputContext* fltextinputcontext_instance = nil;
                                        CGBitmapContextGetBitsPerComponent(gc), CGBitmapContextGetBytesPerRow(gc),
                                        CGBitmapContextGetColorSpace(gc), CGBitmapContextGetBitmapInfo(gc));
   }
+  CGContextClearRect(aux_bitmap, CGRectMake(0, 0,
+                     CGBitmapContextGetWidth(aux_bitmap), CGBitmapContextGetHeight(aux_bitmap)));
   if (r) CGContextScaleCTM(aux_bitmap, 2, 2);
 }
 - (void)reset_aux_bitmap {
diff --git src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx
index ea651cf..e601df1 100644
--- src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx
+++ src/drivers/Cairo/Fl_Cairo_Graphics_Driver.cxx
@@ -31,6 +31,7 @@
 #include <stdlib.h>  // abs(int)
 #include <string.h>  // memcpy()
 
+extern unsigned fl_cmap[256]; // defined in fl_color.cxx
 
 // duplicated from Fl_PostScript.cxx
 struct callback_data {
@@ -331,9 +332,29 @@ void Fl_Cairo_Graphics_Driver::color(unsigned char r, unsigned char g, unsigned
   check_status();
 }
 
-void Fl_Cairo_Graphics_Driver::color(Fl_Color c) {
-  Fl::get_color(c, cr_, cg_, cb_);
-  Fl_Cairo_Graphics_Driver::color(cr_, cg_, cb_);
+void Fl_Cairo_Graphics_Driver::color(Fl_Color i) {
+  Fl_Graphics_Driver::color(i);
+  if (!cairo_) return; // no context yet? We will assign the color later.
+  uchar r, g, b;
+  double fa = 1.0;
+  if (i & 0xFFFFFF00) {
+    // translate rgb colors into color index
+    r = i>>24;
+    g = i>>16;
+    b = i>> 8;
+  } else {
+    // translate index into rgb:
+    unsigned c = fl_cmap[i];
+    c = c ^ 0x000000ff; // trick to restore the color's correct alpha value
+    r = c>>24;
+    g = c>>16;
+    b = c>> 8;
+    fa = (c & 0xff)/255.0;
+  }
+  double fr = r/255.0;
+  double fg = g/255.0;
+  double fb = b/255.0;
+  cairo_set_source_rgba(cairo_, fr, fg, fb, fa);
 }
 
 Fl_Color Fl_Cairo_Graphics_Driver::color() { return Fl_Graphics_Driver::color(); }
diff --git src/drivers/Quartz/Fl_Quartz_Graphics_Driver_color.cxx src/drivers/Quartz/Fl_Quartz_Graphics_Driver_color.cxx
index a4cb37d..9d31f85 100644
--- src/drivers/Quartz/Fl_Quartz_Graphics_Driver_color.cxx
+++ src/drivers/Quartz/Fl_Quartz_Graphics_Driver_color.cxx
@@ -34,6 +34,7 @@ extern unsigned fl_cmap[256]; // defined in fl_color.cxx
 void Fl_Quartz_Graphics_Driver::color(Fl_Color i) {
   Fl_Graphics_Driver::color(i);
   uchar r, g, b;
+  float fa = 1.0f;
   if (i & 0xFFFFFF00) {
     // translate rgb colors into color index
     r = i>>24;
@@ -42,16 +43,20 @@ void Fl_Quartz_Graphics_Driver::color(Fl_Color i) {
   } else {
     // translate index into rgb:
     unsigned c = fl_cmap[i];
+    c = c ^ 0x000000ff; // trick to restore the color's correct alpha value
     r = c>>24;
     g = c>>16;
     b = c>> 8;
+    uchar a = c & 0xff;
+    //printf("i=%d rgb=%u,%u,%u a=%u\n",i,r,g,b,a);
+    fa = a/255.0f;
   }
   if (!gc_) return; // no context yet? We will assign the color later.
   float fr = r/255.0f;
   float fg = g/255.0f;
   float fb = b/255.0f;
-  CGContextSetRGBFillColor(gc_, fr, fg, fb, 1.0f);
-  CGContextSetRGBStrokeColor(gc_, fr, fg, fb, 1.0f);
+  CGContextSetRGBFillColor(gc_, fr, fg, fb, fa);
+  CGContextSetRGBStrokeColor(gc_, fr, fg, fb, fa);
 }
 
 void Fl_Quartz_Graphics_Driver::color(uchar r, uchar g, uchar b) {
diff --git src/fl_color.cxx src/fl_color.cxx
index d28533f..d925a73 100644
--- src/fl_color.cxx
+++ src/fl_color.cxx
@@ -68,6 +68,8 @@ void Fl::set_color(Fl_Color i, uchar red, uchar green, uchar blue) {
  Sets an entry in the fl_color index table.
 
  You can set it to any 8-bit RGBA color.
+ \note The color transparency is effective under the Wayland, hybrid Wayland/X11 and macOS platforms, whereas it has no effect under the X11 and Windows platforms. It's also effective for widgets added to an Fl_Gl_Window.
+ \version 1.4
  */
 void Fl::set_color(Fl_Color i, uchar red, uchar green, uchar blue, uchar alpha) {
   Fl::set_color((Fl_Color)(i & 255),
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.