#HTML Gradient Generator #Author: James Howard (10/6/15) top_text = 'SBCC CS Club ROCKS!!!' background = '#c0ffee' #light turquoise #colors = ('FF0000', 'FFFFFF', '0000FF') #Red, White, Blue colors = ('FF0000', '000000', '0000FF') #Red, White, Blue html_start=''' ''' html_end=''' ''' #background needed to properly nest tags lorem_ipsum='Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' def writeStringToFile(string, filename): f = open(filename, 'w') f.write(string) def intToHex(x): return (2-len(hex(x)[2:]))*'0' + hex(x)[2:].upper() \ if x >=0 and x <= 255 else '00' if x < 0 else 'FF' def hexToInt(x): return int('0x' + x,0) def RGBtoHex(rgb): r,g,b = rgb return intToHex(r) + intToHex(g) + intToHex(b) def hexToRGB(x): return tuple([int('0x' + x[2*pos:2*pos+2],0) for pos in range(len(x)//2)]) def set_background(color='lightgrey'): return '' def header(string): return '

' + string + '

' def paragraph(string, fontsize=8): return '

' + string + '

' def colorize(rgb,string=''): r,g,b = rgb return '' + string def getRGBChangeRates_(text, c1, c2): #deprecated, this was the first solution if len(c1) == 6 and len(c2) == 6: rates = [] for pos in range(3): h1, h2 = c1[2*pos:2*pos+2], c2[2*pos:2*pos+2] begin = hexToInt(h1) end = hexToInt(h2) rates.append((end-begin)/len(text)) return tuple(rates) def getRGBChangeRates(text, c1, c2): return tuple((hexToInt(c2[2*pos:2*pos+2])-hexToInt(c1[2*pos:2*pos+2]))/len(text) \ for pos in range(len(c1)//2)) def twoColorGradient(text, c1, c2, skipFirstCharacter=False): rgb_rates, rgb_base = getRGBChangeRates(text, c1, c2), hexToRGB(c1) result = '' for index in range(len(text)): if skipFirstCharacter and index==0: continue rgb = [rgb_base[channel] + int(rgb_rates[channel]*index) for channel in range(3)] result += colorize(rgb, text[index]) return result def nColorGradient(text, colors): num_segments = len(colors)-1 len_segment = len(text)//num_segments result, firstSegment = '', True for pos in range(num_segments): if pos != num_segments-1: portion = text[pos*len_segment:(pos+1)*len_segment] else: portion = text[pos*len_segment:] portion = portion if firstSegment else ' ' + portion result += twoColorGradient(portion, colors[pos], colors[pos+1], not firstSegment) firstSegment = False return result def generateHTML(str_header, str_paragraph, bg_color='lightgrey'): result = html_start + '\n' + set_background(bg_color) + '\n' + \ header(str_header) + '\n' + paragraph(str_paragraph) + '\n' + \ html_end return result output = nColorGradient(lorem_ipsum, colors) writeStringToFile(generateHTML(top_text, output, background), 'example.html')