Author Topic: Random RGB in as HEX String  (Read 3125 times)

0 Members and 1 Guest are viewing this topic.

David Bethel

  • Swamp Rat
  • Posts: 656
Random RGB in as HEX String
« on: March 31, 2015, 01:28:25 PM »
Greetings,

I'm trying to come up with a way the produce a random RGB color for web base text.

My base thinking is to:

  • create a random number < (expt 16 6) 

    I will probably reduce this number due to the fact that the text will always be in a light background
    The lower the number, the darker the color
  • convert the number into 6 integers (0 -16) using (rem ( expt ) )
  • convert the integers into hex strings
  • ( strcat ) the strings together

Any one have a suggestions as to a better concept ?

Thanks  -David
R12 Dos - A2K

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Random RGB in as HEX String
« Reply #1 on: March 31, 2015, 01:57:59 PM »
The 'why' escapes me...
But I would suggest this improvement to your approach:
1. Create 3 random integers 0-255.
2. Reduces values if required.
3. Convert to hex.
4. Concatenate.

danallen

  • Guest
Re: Random RGB in as HEX String
« Reply #2 on: March 31, 2015, 02:06:18 PM »
Pick a random ACI color then do ACI->RGB conversion? Unless you really need thousands more color options.

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Random RGB in as HEX String
« Reply #3 on: March 31, 2015, 02:31:27 PM »
This will be used by an Autolisp routine that creates HTML pages on the fly.

I thought of the 3 numbers < 255 but I haven't figured out a way to insure a dark enough color "00FFFF" is cyan and shows up well

Maybe if  (+ r g b) < 700 or so could work,  765 would be the max

We use old releases, so we are limited to vanilla Autolsip

Code - Auto/Visual Lisp: [Select]
  1. ;;;CONVERT 2 BIT INTEGER to HEX
  2. (defun int2hex (n / lv rv lc rc)
  3.   (setq lv (fix (/ n 16.)))
  4.   (setq rv (if (zerop lv) n (rem n (* lv 16))))
  5.   (setq lc (cond ((> lv 9) (chr (+ 65 (- lv 10))))
  6.                  (T        (chr (+ 48 lv)))))
  7.   (setq rc (cond ((> rv 9) (chr (+ 65 (- rv 10))))
  8.                  (T        (chr (+ 48 rv)))))
  9.   (strcat lc rc))
  10.  
  11.  

Thanks!  -David
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Random RGB in as HEX String
« Reply #4 on: March 31, 2015, 02:43:21 PM »
Perhaps use random HSL values and ensure the lightness is not greater than a given value.

These functions may help with the task:

Random Number Functions
HSL to RGB
Base Conversion Functions

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Random RGB in as HEX String
« Reply #5 on: March 31, 2015, 04:51:34 PM »
Interesting functions Lee

?  Did you omit the leading ( left ) "0"s in lm:dec->base  function for a reason ?

Code: [Select]
(lm:dec->base 2 16)

returns "2" whereas I was anticipating "02"


I'll digest the HSL parameters

Thanks!  -David

R12 Dos - A2K

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Random RGB in as HEX String
« Reply #6 on: March 31, 2015, 04:57:50 PM »
This is what I was using.

Code: [Select]
(setq fc_rgb '(( 0 . "BLACK")     ( 1 . "FUCHSIA")  ( 2 . "NAVY")
               ( 3 . "MAROON")   ( 4 . "GREEN")    ( 5 . "BLUE")
               ( 6 . "RED")      ( 7 . "PURPLE")   ( 8 . "TEAL")
               ( 9 . "OLIVE")    (10 . "GRAY")     (11 . "AQUA")
               (12 . "LIME")     (13 . "SILVER")
               (14 . "YELLOW")   (15 . "WHITE")))

(setq tc (cdr (assoc gv_fc fc_rgb)))

There just wasn't enough variations.

-David
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Random RGB in as HEX String
« Reply #7 on: March 31, 2015, 05:47:19 PM »
Interesting functions Lee

Thank you David  :-)

Did you omit the leading ( left ) "0"s in lm:dec->base  function for a reason ?

Code: [Select]
(lm:dec->base 2 16)

returns "2" whereas I was anticipating "02"

If leading zeros were to be returned by such a function, how many should be included in all cases?

Hexadecimal values will not always be two digits in length as they are with RGB values; should binary values be padded with zeros to the nearest byte? nearest word?

This is what I was using.
Code: [Select]
(setq fc_rgb '(( 0 . "BLACK")     ( 1 . "FUCHSIA")  ( 2 . "NAVY")
               ( 3 . "MAROON")   ( 4 . "GREEN")    ( 5 . "BLUE")
               ( 6 . "RED")      ( 7 . "PURPLE")   ( 8 . "TEAL")
               ( 9 . "OLIVE")    (10 . "GRAY")     (11 . "AQUA")
               (12 . "LIME")     (13 . "SILVER")
               (14 . "YELLOW")   (15 . "WHITE")))

(setq tc (cdr (assoc gv_fc fc_rgb)))

There just wasn't enough variations.

I recognise that code  :wink: - I have a good idea where this is going to be used  :-)

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Random RGB in as HEX String
« Reply #8 on: April 01, 2015, 07:10:11 AM »


returns "2" whereas I was anticipating "02"

I misspoke here ( as usual )  For 2 byte base 16 values, I can strcat a RightCharacter LeftCharacter

I confused the dec->base vs randrange options

Quote
I recognise that code  :wink: - I have a good idea where this is going to be used  :-)

LOL   I see you went with CSS for coloring your code on yor site.  Looks Good !

This coloring is more about verify a browser's automatic refresh on project web pages.  Mr. Gate's junk IE
defaults to 'Automatic' on it's history refresh setting which is by no means automatic.

I can ask what color the text on the pages is before and after a manual <F5> refresh.  I can update
a project, but client still sees the previous version of the design if the refresh isn't preformed with each visit.


Thanks!  -David
R12 Dos - A2K

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Random RGB in as HEX String
« Reply #9 on: April 01, 2015, 07:29:25 AM »

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Random RGB in as HEX String
« Reply #10 on: April 01, 2015, 07:41:00 AM »
If you want to force a full reload, you could always just clear the cache using JavaScript. Or you could add a reload meta-tag to state that it should be reloaded every so many seconds.

http://www.htmlgoodies.com/tutorials/getting_started/article.php/3479551

E.g. you could add that JS code on the BODY's onload event. Thus each time the page is opened the page's cache is cleared - forcing a full reload. Though going this route would need a delay on that code so it doesn't end in an infinite loop.

BTW, what back-end are you using or are these just straight-forward HTML files?
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Random RGB in as HEX String
« Reply #11 on: April 01, 2015, 07:42:52 AM »
Note: To avoid caching of web pages you can use meta tags:

Maybe a bit too aggressive, but I'll give it a shot.

Comparing the html and a href files time/dates should be the proper way, but the browser wars were ugly and led to some pretty shabby work.

Thanks!  -David
R12 Dos - A2K

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Random RGB in as HEX String
« Reply #12 on: April 01, 2015, 07:53:06 AM »
BTW, what back-end are you using or are these just straight-forward HTML files?
LOL

I'm so old fashioned I make myself embarrassed some time.

Here's a snippet of code ( with the meta tags added ) from the program that creates the HTML files:
Code: [Select]
  (setq hl (cons (strcat "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">") hl))
  (setq hl (cons "<!-- Header -->" hl))
  (setq hl (cons (strcase (strcat "<html><head>") t) hl))
  (setq hl (cons "<!-- Meta Info -->" hl))
  (setq hl (cons (strcase (strcat "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" >") t) hl))
  (setq hl (cons (strcase (strcat "<META HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\">") t) hl))
  (setq hl (cons (strcase (strcat "<META HTTP-EQUIV=\"CACHE-CONTROL\" CONTENT=\"NO-CACHE\">") t) hl))
  (setq hl (cons (strcase (strcat "<META HTTP-EQUIV=\"EXPIRES\" CONTENT=\"-1\">") t) hl))
  (setq hl (cons "<!-- Favicon -->" hl))
  (setq hl (cons (strcase (strcat "<link rel=\"shortcut icon\" href=\"../favicon.ico\" type=\"image/x-icon\">") t) hl))
  (setq hl (cons "<!-- Style Sheet -->" hl))
  (setq hl (cons (strcase (strcat "<link rel=\"stylesheet\" type=\"text/css\" href=\"../proj.css\">") t) hl))
  (setq hl (cons "<!-- Title -->" hl))
  (setq hl (cons (strcat "<title>" (strcase (dwgsname)) " - PROJECT FOLDER </title></head>") hl))


;;;BACKGROUND
  (setq hl (cons "<!-- Background -->" hl))
  (setq hl (cons (strcat "<body bgcolor=\"#dddddd\" text=\"#000000\" link=\"#0000ff\" vlink=\"#993399\" background=\"#dddddd\">") hl))
  (setq hl (cons "<!-- Top Of Page -->" hl))
  (setq hl (cons (strcat "<a name=\"Top\"></a>") hl))


It goes on to :
  • search for certain file names and patterns
  • adds that link's code to the string list
  • creates index.htm from the list

An example :
http://www.davidbethel.com/ngmkt/index.htm

HTML WYSIWYG is a horrible way ( my $0.02 ) to code.

-David
-David
R12 Dos - A2K