Material Library Creation Question
-
I'm just wondering if there is a quick and easy way to read a list of color names and their RGB values and create a library. There's at least 120 colors I'd like to have in a library and I'd rather not have to enter each one by hand.
-
It's fairly easy, can you send the file?
-
Hi Jim,
File has been sent.
Thank you.
-
Here's a quick run-down of how. I'm sure there are better ways, this is the first way that worked. And since it worked, I don't have a need to make it better...
The text file, in this case looks like this (altough Dave's actually was slightly simpler.)
1 Almond #EFDECD (239, 222, 205) 1998 2 Antique Brass #CD9575 (205, 149, 117) 1998 3 Apricot #FDD9B5 (253, 217, 181) 1949 4 Aquamarine #78DBE2 (120, 219, 226) 1958And here's the Ruby code to read and parse the color file (which I called c.txt so that I would never remember what it was for.)
# I'm working in the tmp folder under the Plugins folder pf = Sketchup.find_support_file "Plugins" tf = pf + "/tmp" # a tmp folder # Open and read the file into an Array, using a newline as element separator. # Then, iterate over each element in the array, passing each array element in turn to the variable 'line' # All code between the do..end loop is executed for each line. IO.readlines(tf+"/c.txt").each do |line| # Here I use a regular expression to find text between the first space (\s) and the # symbol. # Then, chop off the # and strip any whitespace before and after the string. name = line[ /\s+(.*?)#/ ].chop.strip # Another regex # Find the text between ( and ), and store it in the 'color' variable. color = line[ /\(.*\)/ ] # substitute the ( ) and with nothing (remove them from string.) # This should really be done with a more correct regex in the previous step. color.gsub!(/[)(]/, '') # split the sdtring on the ',' and store the split values in r, g, and b variables. # split returns an array. Ruby allows multiple assignment for arrays; a, b, c = [1, 2, 3] r, g, b = color.split(',') # Add the material to SketchUp m = Sketchup.active_model.materials.add(name) # Set the materials color. to_i converts the string to an integer. m.color = [r.to_i, g.to_i, b.to_i] end -
@dave r said:
I'm just wondering if there is a quick and easy way to read a list of color names and their RGB values and create a library. There's at least 120 colors I'd like to have in a library and I'd rather not have to enter each one by hand.
Hi dave, do you mind sharing that library of named colors??

-
I wouldn't mind at all but it turned out I didn't need to make it because Jim already did. You can find it at the link to his blog. I was interested in the Crayola crayon colors which he did a few weeks ago.
It just goes to show there's nothing new under the sun.

Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register LoginAdvertisement