Random HTML CSS Hex Color Picker

BustACode 2 Tallied Votes 514 Views Share

I needed a random color selector. I found one here. I rewrote it to make it more general purpose.
"f_GenRandomColor" is the picker. The output is in list form, which should make it suitable for most general applications.
"f_HTMLRandomColorChart" is just a way to create an HTML table to test the output from.

Thanks to the original author.

# Imports #
from __future__ import print_function
import sys, os
from random import randint

    # Main #
def main():
    v_NumColors = int(raw_input("Number of random colors to generate, or [0] for QUIT") or 0) ##
    v_FillerLabel = raw_input("Filler text to use: ")
    v_FileName = raw_input("Save file as: ")

    v_ColorsList = f_GenRandomColor(v_NumColors)
    f_HTMLRandomColorChart(v_FileName, v_FillerLabel, v_ColorsList)

#-----------Basement------------

    # Functions #
        # Normal Functions
def f_GenRandomColor(v_NumColors):
    """&Syntax: INT; Returns: LIST of STRs);
    Requires: from random import randint;
    Desc.: Takes an INT for number colors and returns a list of STR CSS hex colors. I.e. #728308);
    Original Source: http://peepspower.com/python-script-to-generate-random-css-colors/
    Test: print(f_GenRandomColor(5))"""
    v_ColorsList = []
    for i in xrange(v_NumColors):
        v_Color = ["#"]
        for i in range(0, 6):
            v_LetterOrNum = randint(0, 1) ## Random of letter or number
            if v_LetterOrNum == 0:
                # Generate Number
              v_AsciiVal = randint(48, 57) ## Get a random ASCII code value from hex range of numbers
            else:
                # Generate Letter
              v_AsciiVal = randint(65, 70) ## Get a random ASCII code value from hex range of letters
            v_Color.append(chr(v_AsciiVal))
        v_Color = "".join(v_Color)
        v_ColorsList.append(v_Color)
    return v_ColorsList

def f_HTMLRandomColorChart(v_FileName, v_FillerLabel, v_ColorsList):
    v_Conn = open(v_FileName + ".html",'w')
    # Single quotes for this one
    v_Conn.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">')
    v_Conn.write("<html><head><title>Random Color Chart</title></head><body><table border = \"1\"><tr><th>Color Name</th><th>Color</th></tr>\n")

    for i in range(0, len(v_ColorsList)):
        v_Conn.write("<tr><td>" + v_ColorsList[i] + "</td><td style = \"background-color: " + v_ColorsList[i] + ";width:100px\">" + v_FillerLabel + "</td></tr>\n")
    v_Conn.write("</table></body></html>")
    v_Conn.close()

    # Main Loop #
if __name__ == '__main__':
    main()
snippsat 661 Master Poster

Good effort,i give you a up vote:)
But have to mention your code style again,it's not so pretty.
I link to PEP-8 in your post,
just to show a doc that can help you in the future regarding code style.

Here a version i made,
Look a little at how code is formatet,
and names i use in code(No capitale letters,because that is used in class name).

#Testet work in Python 2 and 3
from __future__ import print_function
from random import randint
#Fix so input() work 2 and 3
try:
    input = raw_input
except NameError:
    pass

def random_hex_colors():
    return "#{:06x}".format(randint(0,0xFFFFFF)).upper()

def gen_html_tag(random_hex_colors, number_of_tags, width, filler_text):
    return \
    '\n'.join('<tr><td>{0}</td><td style ='
     '"background-color: {0};width:{1}px">{2}</td></tr>'
    .format(random_hex_colors(), html_width, text_filler)
    for i in range(number_of_tags))

def write_html(tag_gen, file_name):
    with open('{}.html'.format(file_name), 'w') as f_obj:
        f_obj.write('''\
<!DOCTYPE html>
<head>
<title>Random Color Chart</title>
</head>
<table border = "1">
<tr><th>Color Name</th><th>Color</th></tr>
{}
</table>'''.format(tag_gen))

if __name__ == '__main__':
    #Test without user input
    number_of_tags = 3
    html_width = 300
    text_filler = 'test'
    file_name = 'color_test'        
    #Call functions
    tag_gen = gen_html_tag(random_hex_colors,
    number_of_tags, html_width, text_filler)
    write_html(tag_gen, file_name)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.