def make_colour(colour_string, opacity=1.0): if colour_string.startswith('#'): colour = webcolors.hex_to_rgb(colour_string) elif colour_string.startswith('rgb('): rgb = colour_string[4:-1].split(',') if '%' in colour_string: colour = webcolors.rgb_percent_to_rgb(rgb) else: colour = [int(c) for c in rgb] else: colour = webcolors.html5_parse_legacy_color(colour_string) return skia.Color(*tuple(colour), int(255*opacity))
def test_rgb_percent_to_rgb(self): """ Test conversion from percent RGB triplet to integer RGB triplet. """ test_pairs = (((u'100%', u'100%', u'0%'), (255, 255, 0)), ((u'0%', u'0%', u'50%'), (0, 0, 128)), ((u'85.49%', u'64.71%', u'12.5%'), (218, 165, 32))) for pair in test_pairs: self.assertEqual(pair[1], webcolors.rgb_percent_to_rgb(pair[0]))
def test_rgb_percent_to_rgb(self): """ Test conversion from percent RGB triplet to integer RGB triplet. """ test_pairs = (((u'100%', u'100%', u'0%'), (255, 255, 0)), ((u'0%', u'0%', u'50%'), (0, 0, 128)), ((u'85.49%', u'64.71%', u'12.5%'), (218, 165, 32))) for triplet, int_triplet in test_pairs: self.assertEqual(int_triplet, webcolors.rgb_percent_to_rgb(triplet))
def test_rgb_percent_to_rgb(self): """ Test conversion from percent RGB triplet to integer RGB triplet. """ test_pairs = ( ((u"100%", u"100%", u"0%"), (255, 255, 0)), ((u"0%", u"0%", u"50%"), (0, 0, 128)), ((u"85.49%", u"64.71%", u"12.5%"), (218, 165, 32)), ) for triplet, int_triplet in test_pairs: self.assertEqual(int_triplet, webcolors.rgb_percent_to_rgb(triplet))
def test_rgb_percent_to_rgb(self): """ Test conversion from percent RGB triplet to integer RGB triplet. """ test_pairs = (((u'100%', u'100%', u'0%'), (255, 255, 0)), ((u'0%', u'0%', u'50%'), (0, 0, 128)), ((u'85.49%', u'64.71%', u'12.5%'), (218, 165, 32))) for triplet, int_triplet in test_pairs: result = webcolors.rgb_percent_to_rgb(triplet) self.assertTrue(isinstance(result, webcolors.IntegerRGB)) self.assertEqual(int_triplet, result)
async def remove(self, ctx, colour: str): try: rgb = webcolors.name_to_rgb(colour)[0] except: try: rgb = webcolors.hex_to_rgb(colour) except: try: rgb = webcolors.rgb_percent_to_rgb(colour) except: try: rgb = webcolors.rgb_to_name(colour) rgb = colour except: raise commands.BadArgument("Colour hex, name, RGB or RGB percent not found.")
def test_rgb_percent_to_rgb(self): """ Test conversion from percent RGB triplet to integer RGB triplet. """ test_pairs = ( (("100%", "100%", "0%"), (255, 255, 0)), (("0%", "0%", "50%"), (0, 0, 128)), (("85.49%", "64.71%", "12.5%"), (218, 165, 32)), ) for triplet, int_triplet in test_pairs: result = webcolors.rgb_percent_to_rgb(triplet) assert isinstance(result, webcolors.IntegerRGB) assert int_triplet == result
def test_rgb_percent_to_rgb(self): """ Test conversion from percent RGB triplet to integer RGB triplet. """ test_pairs = ( ((u'100%', u'100%', u'0%'), (255, 255, 0)), ((u'0%', u'0%', u'50%'), (0, 0, 128)), ((u'85.49%', u'64.71%', u'12.5%'), (218, 165, 32)) ) for triplet, int_triplet in test_pairs: result = webcolors.rgb_percent_to_rgb(triplet) assert isinstance(result, webcolors.IntegerRGB) assert int_triplet == result
def test_triplet_conversion(self): for int_triplet in int_colors(): self.assertEqual( int_triplet, webcolors.rgb_percent_to_rgb( webcolors.rgb_to_rgb_percent(int_triplet)))
def test_triplet_conversion(self): for int_triplet in int_colors(): conversion = webcolors.rgb_percent_to_rgb( webcolors.rgb_to_rgb_percent(int_triplet)) assert int_triplet == conversion
def do_POST(self): # command................. OPEN_PDF = "OPEN_PDF" NEXT = "NEXT" PREVIOUS = "PREVIOUS" ANNOTS_TYPE = "ANNOTS_TYPE" SPEC_ANNOTS = "SPEC_ANNOTS" GET_PAGE = "GET_PAGE" # ...................... """Handle a post request by returning the square of the number.""" length = int(self.headers.getheader('content-length')) xml = self.rfile.read(length) root = ElementTree.fromstring(xml) xml_cmd = root.find("command") attr = xml_cmd.attrib cmd = attr['name'] print "**********************************************************" if cmd == OPEN_PDF: filename = xml_cmd.find("filename").text print "receive < OPEN_PDF > command with argument: " + filename #pdf.open_file(filename) elif cmd == PREVIOUS: print "receive < PREVIOUS > command" elif cmd == NEXT: print "receive < NEXT > command" elif cmd == ANNOTS_TYPE: print "receive < ANNOTS_TYPE >" #print pdf.get_annot_types() elif cmd == SPEC_ANNOTS: print "receive <SPEC_ANNOTS> command with arguments:" + xml_cmd.find("spec_annots").text elif cmd == GET_PAGE: page = xml_cmd.find('page').text print "receive <GET_PAGE> command with argument" + page elif cmd == "TARGET_PDF": print "receive <TARGET_PDF> command" print target_pdf self.wfile.write(target_pdf) elif cmd == "Annots": print "receive <Annots>" SEPANNOT = "%AnNot!" SEPITEM = "%ItEm!" annots_str = xml_cmd.find("data").text.split(SEPANNOT) # content, type, rgb, rect(minX, minY, maxX, *maxY), page annots = [] for annot in annots_str: items = annot.strip(' ').split(SEPITEM) if len(items) == 5: rect = tuple(float(v) for v in items[3].split(',')) rgb = tuple(float(v) for v in items[2].split(',')) rgb = tuple(str(v*100)+'%' for v in rgb) # rgb tuple (int, int, int) rgb = webcolors.rgb_percent_to_rgb(rgb) # minx < maxx, miny < maxy assert rect[0] < rect[2] and rect[1] < rect[3] a = AnnotStruct(items[0].encode('utf-8'), items[1], rgb, rect, int(items[4])) annots.append(a) # sort annotations according to page index etc. annots.sort() # map the annot to the losest annotation configured for annot in annots: annot.rgb = pdf_annot_to_sys_annot(annot)[1] fname = util.path_fname(target_pdf)[1].rstrip('.pdf') annots2mindmap(annots, fname + ".xmind") # write to ".annot" file #f = open(fname + ".annot" , 'w') #print "save to file:", fname + ".txt" #for annot in annots: #print >> f, "**************************************************" #print >> f, annot.content ##print "**************************************************" ##print annot.content #annot_types.add((annot.mtype, annot.rgb)) self.wfile.write("to browser") #print "************annotations types:*************" #print annot_types print "Done!" else: print "error:Unknown Command."
def test_triplet_conversion(self): for int_triplet in int_colors(): conversion = webcolors.rgb_percent_to_rgb( webcolors.rgb_to_rgb_percent(int_triplet) ) assert int_triplet == conversion
async def add(self, ctx, *, colour): if not self.bot.get_config(ctx.guild.id, 'allow_colours'): return await ctx.send(f'Please enable colours with `colours allow`') try: rgb = webcolors.name_to_rgb(colour) except: try: rgb = tuple(int(colour.lstrip('#')[i:i + 2], 16) for i in (0, 2, 4)) except: try: rgb = webcolors.rgb_percent_to_rgb(colour) except: try: col = colour.split(',') numbers = [] for n in col: try: s = n.replace("(", "") s = s.replace(")", "") int(s) numbers.append(int(s)) except: pass tup = tuple(numbers) rgb = webcolors.rgb_to_name(tup) rgb = tup except Exception as e: print(e) raise commands.BadArgument(f"Colour hex, name, RGB or RGB percent {colour} not found.") try: colourname = webcolors.rgb_to_name(rgb) colourhex = webcolors.rgb_to_hex(rgb) except: colourname = 'No Name' colourhex = 'No Hex' existing = self.bot.get_colour(ctx.guild.id, rgb) if existing: role = self.bot.get_role(existing['roleid']) return await ctx.author.add_roles(role, reason="MathsBot colour command") if self.bot.get_config(ctx.guild.id, 'colour_hoisted'): hoist = ctx.me.top_role.position - 1 desc = f'Role hoisted below {ctx.me.top_role.name}.' else: hoist = 1 desc = 'Role not hoisted. To enable hoisting use `colour hoist`' try: role = await ctx.guild.create_role(name=f"MB: {colourname} - {colourhex} ", colour=discord.Colour.from_rgb(rgb[0], rgb[1], rgb[2]), reason=f"New MathsBot colour role: {colourname} - {colourhex}" f" by user {ctx.author.display_name}#{ctx.author.discriminator}") await role.edit(mentionable=False, position=hoist) except: raise commands.BotMissingPermissions("I need `manage_roles` permission to do this!") e = discord.Embed(colour=discord.Colour.from_rgb(rgb[0], rgb[1], rgb[2])) e.description = f'Colour: {colourname}\n' \ f'Hex: {colourhex}\n' \ f'RGB: {rgb}\n'\ f'{desc}' e.set_footer(text="If you want colours hoisted higher/lower, change my top role hoisting " "and run `colour refresh`") await ctx.send(embed=e) await asyncio.sleep(1) await ctx.author.add_roles(role, reason="MathsBot colour command")
def test_triplet_conversion(self): for int_triplet in int_colors(): self.assertEqual(int_triplet, webcolors.rgb_percent_to_rgb( webcolors.rgb_to_rgb_percent(int_triplet)))