From: Alan Third Date: Sat, 14 Aug 2021 15:11:30 +0000 (+0100) Subject: Simplify NS color handling X-Git-Tag: emacs-29.0.90~3504 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=0f427befe82f88237bdccbd528baf76f6d6485b0;p=emacs.git 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. --- diff --git a/src/image.c b/src/image.c index dd5ea19fc15..dc9aae27c92 100644 --- a/src/image.c +++ b/src/image.c @@ -80,14 +80,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) @@ -96,6 +89,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 81019fce09d..365df7fc1e8 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 ce8f5949024..8bd7611441f 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 4e5ce5ef700..12f24fbfd53 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 @@ -4933,8 +4922,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; @@ -5010,6 +4998,7 @@ static void ns_delete_display (struct ns_display_info *dpyinfo) { /* TODO... */ + [dpyinfo->color_table release]; } @@ -5223,10 +5212,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]]; }