emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

scratch/ns/testing 2dd414a 2/7: Simplify NS color handling


From: Alan Third
Subject: scratch/ns/testing 2dd414a 2/7: Simplify NS color handling
Date: Sun, 14 Nov 2021 13:50:26 -0500 (EST)

branch: scratch/ns/testing
commit 2dd414a0ddd7710a3154b33596bece43e322cdce
Author: Alan Third <alan@idiocy.org>
Commit: Alan Third <alan@idiocy.org>

    Simplify NS color handling
    
    * src/image.c (RGB_TO_ULONG):
    (ARGB_TO_ULONG):
    (RED_FROM_ULONG):
    (RED16_FROM_ULONG):
    (GREEN16_FROM_ULONG):
    (BLUE16_FROM_ULONG): Define these here for NS too.
    * src/nsfns.m (ns_set_foreground_color):
    (ns_set_background_color): Use new EmacsColor methods.
    * src/nsterm.h (struct ns_color_table): Replace this struct with a
    built-in Objective C type.
    (RGB_TO_ULONG):
    (ARGB_TO_ULONG):
    (ALPHA_FROM_ULONG):
    (RED_FROM_ULONG):
    (GREEN_FROM_ULONG):
    (BLUE_FROM_ULONG):
    (RED16_FROM_ULONG):
    (GREEN16_FROM_ULONG):
    (BLUE16_FROM_ULONG): These are no longer needed in the NS specific code.
    (struct ns_display_info): Use an NSMutableArray instead of a custom struct.
    * src/nsterm.m ([NSColor colorWithUnsignedLong:hasAlpha:]):
    ([NSColor unsignedLong]): New methods.
    (ns_lookup_indexed_color):
    (ns_index_color): Use the NSMutableArray lookup table.
    (ns_term_init):
    (ns_color_index_to_rgba):
    (ns_query_color): Use the new EmacsColor methods.
    (ns_initialize_display_info):
    (ns_delete_display): Initialize and release the NSMutableArray lookup table.
---
 src/image.c  | 18 ++++++------
 src/nsfns.m  | 18 +++---------
 src/nsterm.h | 37 ++++--------------------
 src/nsterm.m | 94 ++++++++++++++++++++++++++----------------------------------
 4 files changed, 60 insertions(+), 107 deletions(-)

diff --git a/src/image.c b/src/image.c
index 6769e49..c89798e 100644
--- a/src/image.c
+++ b/src/image.c
@@ -78,14 +78,7 @@ typedef struct x_bitmap_record Bitmap_Record;
 #endif /* !USE_CAIRO */
 #endif /* HAVE_X_WINDOWS */
 
-#ifdef USE_CAIRO
-#define GET_PIXEL image_pix_context_get_pixel
-#define PUT_PIXEL image_pix_container_put_pixel
-#define NO_PIXMAP 0
-
-#define PIX_MASK_RETAIN        0
-#define PIX_MASK_DRAW  255
-
+#if defined(USE_CAIRO) || defined(HAVE_NS)
 #define RGB_TO_ULONG(r, g, b) (((r) << 16) | ((g) << 8) | (b))
 #define ARGB_TO_ULONG(a, r, g, b) (((a) << 24) | ((r) << 16) | ((g) << 8) | 
(b))
 #define RED_FROM_ULONG(color)  (((color) >> 16) & 0xff)
@@ -94,6 +87,15 @@ typedef struct x_bitmap_record Bitmap_Record;
 #define RED16_FROM_ULONG(color)                (RED_FROM_ULONG (color) * 0x101)
 #define GREEN16_FROM_ULONG(color)      (GREEN_FROM_ULONG (color) * 0x101)
 #define BLUE16_FROM_ULONG(color)       (BLUE_FROM_ULONG (color) * 0x101)
+#endif
+
+#ifdef USE_CAIRO
+#define GET_PIXEL image_pix_context_get_pixel
+#define PUT_PIXEL image_pix_container_put_pixel
+#define NO_PIXMAP 0
+
+#define PIX_MASK_RETAIN        0
+#define PIX_MASK_DRAW  255
 
 static unsigned long image_alloc_image_color (struct frame *, struct image *,
                                              Lisp_Object, unsigned long);
diff --git a/src/nsfns.m b/src/nsfns.m
index f4d8172..120294e 100644
--- a/src/nsfns.m
+++ b/src/nsfns.m
@@ -236,7 +236,6 @@ static void
 ns_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
 {
   NSColor *col;
-  EmacsCGFloat r, g, b, alpha;
 
   /* Must block_input, because ns_lisp_to_color does block/unblock_input
      which means that col may be deallocated in its unblock_input if there
@@ -253,12 +252,7 @@ ns_set_foreground_color (struct frame *f, Lisp_Object arg, 
Lisp_Object oldval)
   [f->output_data.ns->foreground_color release];
   f->output_data.ns->foreground_color = col;
 
-  [col getRed: &r green: &g blue: &b alpha: &alpha];
-  FRAME_FOREGROUND_PIXEL (f) =
-    ARGB_TO_ULONG ((unsigned long) (alpha * 0xff),
-                   (unsigned long) (r * 0xff),
-                   (unsigned long) (g * 0xff),
-                   (unsigned long) (b * 0xff));
+  FRAME_FOREGROUND_PIXEL (f) = [col unsignedLong];
 
   if (FRAME_NS_VIEW (f))
     {
@@ -277,7 +271,7 @@ ns_set_background_color (struct frame *f, Lisp_Object arg, 
Lisp_Object oldval)
   struct face *face;
   NSColor *col;
   NSView *view = FRAME_NS_VIEW (f);
-  EmacsCGFloat r, g, b, alpha;
+  EmacsCGFloat alpha;
 
   block_input ();
   if (ns_lisp_to_color (arg, &col))
@@ -291,12 +285,8 @@ ns_set_background_color (struct frame *f, Lisp_Object arg, 
Lisp_Object oldval)
   [f->output_data.ns->background_color release];
   f->output_data.ns->background_color = col;
 
-  [col getRed: &r green: &g blue: &b alpha: &alpha];
-  FRAME_BACKGROUND_PIXEL (f) =
-    ARGB_TO_ULONG ((unsigned long) (alpha * 0xff),
-                   (unsigned long) (r * 0xff),
-                   (unsigned long) (g * 0xff),
-                   (unsigned long) (b * 0xff));
+  FRAME_BACKGROUND_PIXEL (f) = [col unsignedLong];
+  alpha = [col alphaComponent];
 
   if (view != nil)
     {
diff --git a/src/nsterm.h b/src/nsterm.h
index 8175f99..add8e38 100644
--- a/src/nsterm.h
+++ b/src/nsterm.h
@@ -358,7 +358,7 @@ typedef id instancetype;
 + (NSColor *)colorForEmacsRed:(CGFloat)red green:(CGFloat)green
                          blue:(CGFloat)blue alpha:(CGFloat)alpha;
 - (NSColor *)colorUsingDefaultColorSpace;
-
+- (unsigned long)unsignedLong;
 @end
 
 
@@ -766,35 +766,6 @@ struct ns_bitmap_record
   int height, width, depth;
 };
 
-/* This maps between emacs color indices and NSColor objects.  */
-struct ns_color_table
-{
-  ptrdiff_t size;
-  ptrdiff_t avail;
-#ifdef __OBJC__
-  NSColor **colors;
-  NSMutableSet *empty_indices;
-#else
-  void **items;
-  void *availIndices;
-#endif
-};
-#define NS_COLOR_CAPACITY 256
-
-#define RGB_TO_ULONG(r, g, b) (((r) << 16) | ((g) << 8) | (b))
-#define ARGB_TO_ULONG(a, r, g, b) (((a) << 24) | ((r) << 16) | ((g) << 8) | 
(b))
-
-#define ALPHA_FROM_ULONG(color) ((color) >> 24)
-#define RED_FROM_ULONG(color) (((color) >> 16) & 0xff)
-#define GREEN_FROM_ULONG(color) (((color) >> 8) & 0xff)
-#define BLUE_FROM_ULONG(color) ((color) & 0xff)
-
-/* Do not change `* 0x101' in the following lines to `<< 8'.  If
-   changed, image masks in 1-bit depth will not work.  */
-#define RED16_FROM_ULONG(color) (RED_FROM_ULONG(color) * 0x101)
-#define GREEN16_FROM_ULONG(color) (GREEN_FROM_ULONG(color) * 0x101)
-#define BLUE16_FROM_ULONG(color) (BLUE_FROM_ULONG(color) * 0x101)
-
 #ifdef NS_IMPL_GNUSTEP
 /* this extends font backend font */
 struct nsfont_info
@@ -850,7 +821,11 @@ struct ns_display_info
   ptrdiff_t bitmaps_size;
   ptrdiff_t bitmaps_last;
 
-  struct ns_color_table *color_table;
+#ifdef __OBJC__
+  NSMutableArray *color_table;
+#else
+  void *color_table;
+#endif
 
   /* DPI resolution of this screen */
   double resx, resy;
diff --git a/src/nsterm.m b/src/nsterm.m
index e29dda6..100ac19 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -166,6 +166,29 @@ char const * nstrace_fullscreen_type_name (int fs_type)
   return [self colorUsingColorSpace: [NSColorSpace deviceRGBColorSpace]];
 }
 
++ (NSColor *)colorWithUnsignedLong:(unsigned long)c
+                          hasAlpha:(BOOL)alpha
+{
+  EmacsCGFloat a = (double)((c >> 24) & 0xff) / 255.0;
+  EmacsCGFloat r = (double)((c >> 16) & 0xff) / 255.0;
+  EmacsCGFloat g = (double)((c >> 8) & 0xff) / 255.0;
+  EmacsCGFloat b = (double)(c & 0xff) / 255.0;
+
+  return [NSColor colorForEmacsRed:r green:g blue:b
+                             alpha:(alpha ? a : (EmacsCGFloat)1.0)];
+}
+
+- (unsigned long)unsignedLong
+{
+  EmacsCGFloat r, g, b, a;
+  [self getRed:&r green:&g blue:&b alpha:&a];
+
+  return (((unsigned long) (a * 255)) << 24)
+    | (((unsigned long) (r * 255)) << 16)
+    | (((unsigned long) (g * 255)) << 8)
+    | ((unsigned long) (b * 255));
+}
+
 @end
 
 /* ==========================================================================
@@ -1952,53 +1975,29 @@ ns_fullscreen_hook (struct frame *f)
 NSColor *
 ns_lookup_indexed_color (unsigned long idx, struct frame *f)
 {
-  struct ns_color_table *color_table = FRAME_DISPLAY_INFO (f)->color_table;
-  if (idx < 1 || idx >= color_table->avail)
+  NSMutableArray *color_table = FRAME_DISPLAY_INFO (f)->color_table;
+  if (idx < 1 || idx >= [color_table count])
     return nil;
-  return color_table->colors[idx];
+  return [color_table objectAtIndex:idx];
 }
 
 
 unsigned long
 ns_index_color (NSColor *color, struct frame *f)
 {
-  struct ns_color_table *color_table = FRAME_DISPLAY_INFO (f)->color_table;
-  ptrdiff_t idx;
-  ptrdiff_t i;
+  NSMutableArray *color_table = FRAME_DISPLAY_INFO (f)->color_table;
 
-  if (!color_table->colors)
-    {
-      color_table->size = NS_COLOR_CAPACITY;
-      color_table->avail = 1; /* skip idx=0 as marker */
-      color_table->colors = xmalloc (color_table->size * sizeof (NSColor *));
-      color_table->colors[0] = nil;
-      color_table->empty_indices = [[NSMutableSet alloc] init];
-    }
+  /* An index of 0 appears to be special in some way, so insert a
+     dummy object.  */
+  if ([color_table count] == 0)
+    [color_table addObject:[NSNull null]];
 
   /* Do we already have this color?  */
-  for (i = 1; i < color_table->avail; i++)
-    if (color_table->colors[i] && [color_table->colors[i] isEqual: color])
-      return i;
-
-  if ([color_table->empty_indices count] > 0)
-    {
-      NSNumber *index = [color_table->empty_indices anyObject];
-      [color_table->empty_indices removeObject: index];
-      idx = [index unsignedLongValue];
-    }
-  else
-    {
-      if (color_table->avail == color_table->size)
-       color_table->colors =
-         xpalloc (color_table->colors, &color_table->size, 1,
-                  min (ULONG_MAX, PTRDIFF_MAX), sizeof *color_table->colors);
-      idx = color_table->avail++;
-    }
+  if ([color_table containsObject:color])
+    return [color_table indexOfObject:color];
 
-  color_table->colors[idx] = color;
-  [color retain];
-  /* fprintf(stderr, "color_table: allocated %d\n",idx); */
-  return idx;
+  [color_table addObject:color];
+  return [color_table count] - 1;
 }
 
 
@@ -2136,13 +2135,7 @@ ns_color_index_to_rgba(int idx, struct frame *f)
   NSColor *col;
   col = ns_lookup_indexed_color (idx, f);
 
-  EmacsCGFloat r, g, b, a;
-  [col getRed: &r green: &g blue: &b alpha: &a];
-
-  return ARGB_TO_ULONG((unsigned long) (a * 255),
-                       (unsigned long) (r * 255),
-                       (unsigned long) (g * 255),
-                       (unsigned long) (b * 255));
+  return [col unsignedLong];
 }
 
 void
@@ -2161,11 +2154,7 @@ ns_query_color(void *col, Emacs_Color *color_def, bool 
setPixel)
   color_def->blue  = b * 65535;
 
   if (setPixel == YES)
-    color_def->pixel
-      = ARGB_TO_ULONG((unsigned long) (a * 255),
-                      (unsigned long) (r * 255),
-                      (unsigned long) (g * 255),
-                      (unsigned long) (b * 255));
+    color_def->pixel = [(NSColor *)col unsignedLong];
 }
 
 bool
@@ -4909,8 +4898,7 @@ ns_initialize_display_info (struct ns_display_info 
*dpyinfo)
                 && ![NSCalibratedWhiteColorSpace isEqualToString:
                                                  NSColorSpaceFromDepth 
(depth)];
     dpyinfo->n_planes = NSBitsPerPixelFromDepth (depth);
-    dpyinfo->color_table = xmalloc (sizeof *dpyinfo->color_table);
-    dpyinfo->color_table->colors = NULL;
+    dpyinfo->color_table = [[NSMutableArray array] retain];
     dpyinfo->root_window = 42; /* A placeholder.  */
     dpyinfo->highlight_frame = dpyinfo->ns_focus_frame = NULL;
     dpyinfo->n_fonts = 0;
@@ -4986,6 +4974,7 @@ static void
 ns_delete_display (struct ns_display_info *dpyinfo)
 {
   /* TODO...  */
+  [dpyinfo->color_table release];
 }
 
 
@@ -5199,10 +5188,7 @@ ns_term_init (Lisp_Object display_name)
             name = XCAR (color);
             c = XFIXNUM (XCDR (color));
             [cl setColor:
-                  [NSColor colorForEmacsRed: RED_FROM_ULONG (c) / 255.0
-                                      green: GREEN_FROM_ULONG (c) / 255.0
-                                       blue: BLUE_FROM_ULONG (c) / 255.0
-                                      alpha: 1.0]
+                  [NSColor colorWithUnsignedLong:c hasAlpha:NO]
                   forKey: [NSString stringWithLispString: name]];
           }
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]