Setting Cells to RGB (or hex) color in Numbers?

I'm trying to take a list of Color Codes and spit out a chart with the colors actually visible.


I'm trying the script* in this post:

https://discussions.apple.com/thread/251970289

(It said to start a new thread rather than just continue there)


The script worked once on a sample.. but then not again (and it didn't work quite like I expected)


This is what my table (sample) looks like**:


When I ran it and it worked, once, it gave me this


Why the first cell that has no "#" which should be the trigger according to the script colored as well, I don't know...but as I said, it didn't work a second time


Can anyone help me figure this all out?



*This is the script, by SGIII , I'm trying reposted for convience:

property hexChrs : "0123456789abcdef"

tell application "Numbers"
	tell front document
		tell active sheet
			tell (first table whose class of selection range is range)
				set outList to {}
				repeat with aCell in cells of (get selection range)
					set v to value of aCell
					if text 1 of v is "#" then set v to text 2 thru -1 of v
					set hexColor to my hexColorToRGB(v)
					set inverseColor to {}
					repeat with i from 1 to 3
						copy (65535 - (item i of hexColor)) to end of inverseColor
					end repeat
					if v is "808080" then set inverseColor to {65535, 65535, 65535} -- hack to adjust gray
					set background color of aCell to hexColor
					set text color of aCell to inverseColor
				end repeat
			end tell
		end tell
	end tell
end tell

return outList

on hexColorToRGB(h) -- convert 
	if (count h) < 6 then my badColor(h)
	set rgbColor to {}
	try
		repeat with i from 1 to 6 by 2
			set end of rgbColor to ((my getHexVal(text i of h)) * 16 + (my getHexVal(text (i + 1) of h))) * 257
		end repeat
	end try
	if (count rgbColor) < 3 then my badColor(h)
	return rgbColor
end hexColorToRGB

on getHexVal(c)
	if c is not in hexChrs then error
	set text item delimiters to c
	return (count text item 1 of hexChrs)
end getHexVal

on badColor(n)
	display alert n & " is not a valid hex color" buttons {"OK"} cancel button "OK"
end badColor






**(I changed my table to match the look in the original post to simplify my understanding of the code.....this is the actual table:



Posted on Sep 27, 2021 12:52 PM

Reply
Question marked as Top-ranking reply

Posted on Sep 27, 2021 6:53 PM

With a bunch of iterations and testing and kibitzing in discord and forums, I've written new code that does what I want, using the original data set.


It's running now, and hopefully will actually finish on the full data set, vs the small test data set where it works fine.


tell application "Numbers"
	activate
	tell document 1
		tell active sheet
			set thisTable to first table
			tell thisTable
				set the width of fifth column to 500
				#set the height of every row to 24
				set alignment of every cell to center
				set vertical alignment of every cell to center
				repeat with i from 2 to the count of rows
					try
						set ColorName to value of first cell of row i
						set RVariable to (65535 * (value of second cell of row i))
						set GVariable to (65535 * (value of third cell of row i))
						set BVariable to (65535 * (value of fourth cell of row i))
						set BGColor to {RVariable, GVariable, BVariable}
						set the background color of fifth cell of row i to BGColor
						set value of fifth cell of row i to ColorName as text
						
						set InverseColor to {}
						repeat with iTwo from 1 to 3
							copy (65535 - (item iTwo of BGColor)) to end of InverseColor
						end repeat
						
						set the text color of fifth cell of row i to InverseColor
						#log InverseColor
					end try
					
				end repeat
			end tell
		end tell
	end tell
end tell




3 replies
Question marked as Top-ranking reply

Sep 27, 2021 6:53 PM in response to Todd Kogutt

With a bunch of iterations and testing and kibitzing in discord and forums, I've written new code that does what I want, using the original data set.


It's running now, and hopefully will actually finish on the full data set, vs the small test data set where it works fine.


tell application "Numbers"
	activate
	tell document 1
		tell active sheet
			set thisTable to first table
			tell thisTable
				set the width of fifth column to 500
				#set the height of every row to 24
				set alignment of every cell to center
				set vertical alignment of every cell to center
				repeat with i from 2 to the count of rows
					try
						set ColorName to value of first cell of row i
						set RVariable to (65535 * (value of second cell of row i))
						set GVariable to (65535 * (value of third cell of row i))
						set BVariable to (65535 * (value of fourth cell of row i))
						set BGColor to {RVariable, GVariable, BVariable}
						set the background color of fifth cell of row i to BGColor
						set value of fifth cell of row i to ColorName as text
						
						set InverseColor to {}
						repeat with iTwo from 1 to 3
							copy (65535 - (item iTwo of BGColor)) to end of InverseColor
						end repeat
						
						set the text color of fifth cell of row i to InverseColor
						#log InverseColor
					end try
					
				end repeat
			end tell
		end tell
	end tell
end tell




Sep 27, 2021 9:41 PM in response to Todd Kogutt

I updated the finalized code with comments to explain my mad thinking if someone finds this in a search:


(*  
Script for NUMBERS to add a color swatch to a cell of a row with RGB info in it.
the table this is written for has R,G,and B in it's own cells (Columns B,C,D) in float (0-1)format. It puts the swatch in Column F and then adds the Color name or code (from Column A) into the cell in an inversed color

*)




tell application "Numbers"
	activate
	with timeout of 3600 seconds #Script reads thru whole table cell by cell, apparantly, before excuting.  Will time out without this.
		tell document 1
			tell active sheet
				set thisTable to first table
				try
					tell thisTable
						
						### Formatting commands for table appearance ###
						set the width of fifth column to 500
						#set the height of every row to 24
						set alignment of every cell to center
						set vertical alignment of every cell to center
						### End Formatting Commands ###
						
						
						repeat with i from 2 to the count of rows
							try #Catches cells without values in them, otherwise would error on calculations
								set ColorName to value of first cell of row i
								set RVariable to (65535 * (value of second cell of row i))
								set GVariable to (65535 * (value of third cell of row i))
								set BVariable to (65535 * (value of fourth cell of row i))
								set BGColor to {RVariable, GVariable, BVariable}
								set the background color of fifth cell of row i to BGColor
								
								
								### Section adds the ColorScale name from the first cell into swatch cell in an inversed color for clarity --- (needs some adjustment for greys) ###
								set value of fifth cell of row i to ColorName as text
								set InverseColor to {}
								repeat with iTwo from 1 to 3
									copy (65535 - (item iTwo of BGColor)) to end of InverseColor
								end repeat
								set the text color of fifth cell of row i to InverseColor
								#log InverseColor
								### End Colorscale name section ###
								
							end try
						end repeat
					end tell
				end try
			end tell
		end tell
	end timeout
end tell

# Beagle for Luck
#                 ___
#                /      \__
#|\            /      @   \
#\ \______|      \   .:|)
# \       ##|      | \__/
#  |  ####\__/   \
#  /   /  ##           \
# /   _________\    \
# L_JJ                \__JJ

# beagle should look right in compiled applescript editor

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Setting Cells to RGB (or hex) color in Numbers?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.