Package: acrylic
Have you ever wanted a simple and intuitive way to work with colors in python? Then this library is for you!acrylicis a python package that you can use to manage colors, convert between different color formats, and work with color schemes and palettes.Currently supported color formats are:rgb,hsl,hsv,ryb,hex,nameSmall example:fromacrylicimportColor,RANDOM#  Define a color using rgb valuesorange=Color(rgb=[247,177,79])#  Use saturation from that color to create a new random color with hsvrandom_color=Color(hsv=[RANDOM,orange.hsv.s,98])#  Print the random color's value in hexprint(random_color.hex)# Output: '#50FAF0'check outmore examplesbelow.acrylicalso has support forcolor
schemes, support for more
color schemes and functions to generate color palettes will be added in the
future.complementary=cyan.scheme(Schemes.COMPLEMENTARY)shades=cyan.scheme(Schemes.SHADES)color_palette=[cyan,*complementary,*shades]More about color schemeshereHow to Installacryliccan be installed using pip:pipinstallacrylicIt has no dependencies and works with Python >=3.6DocumentationDefining ColorsYou can create a new color like this:fromacrylicimportColorcyan=Color(rgb=[83,237,229])The same syntax can be used to give input in any of the supported color formats. Currently supported formats arergb,hsv,hsl,hexandryb. Example:color=Color(rgb=[127,255,212])color=Color(hsl=[160,100,75])color=Color(hsv=[160,50,100])color=Color(hex='#7fffd4')color=Color(name='aquamarine')color=Color(ryb=[0,77,128])All values forrgbandrybshould be between0-255The value of hue forhsvandhslshould be between0.0-360.0and the other two components should be between0.0-100.0.Values forhexshould be a string representing 6-digit hex numberValues fornameshould be a string representing a valid CSS3 color nameConverting between color formatsAny instance ofColor()is automatically converted to every supported color format when its created, so there is no need to manually convert from one format to another. For any color, no matter how it was created, you can get its value in any format like this:cyan=Color(rgb=[83,237,229])print(cyan.rgb)print(cyan.hsv)print(cyan.hsl)print(cyan.hex)print(cyan.name)print(cyan.ryb)This makes converting from sayrgbtohslas easy as doing:hsl_values=Color(rgb=[83,237,229]).hslAccessing values of colorsWhen accessing these attributes for a color, it returns the values back as anamedtupleinstance. This behaves exactly as a normaltuplewould, but has an added benefit that its values can be accessed directly via the dot notation. Example:>>>cyan=Color(rgb=[83,237,229])>>>cyan.rgb# returns a namedtuple containing the valuesRgb(r=83,g=237,b=229)>>>[xforxincyan.rgb]# can be iterated over like a normal tuple[83,237,229]>>>cyan.rgb[1]# items can be accessed via index237>>>r,g,b=cyan.rgb# items can be unpacked>>>cyan.rgb.r,cyan.rgb.g# items can also be accessed via their name(83,237)Additional ways to define a colorIn addition to the default way to create a color,Color()offers additional methods that would enhance your ability to create colors.For example, to create a random color:fromacrylicimportColor,RANDOMrandom_color=Color(rgb=RANDOM)Creating a color with a random hue, but fixed saturation and value:random_hue=Color(hsv=[RANDOM,65,95])(for aesthetically pleasing random colors, checkexample 2below)Any of the components can be given as a list of 2 values like[a, b]instead of a single value. When given a range, a valuea <= value <= bwill randomly be picked for that component. For example to create a cyan color where saturation is randomly picked between 30 to 70:random_cyan=Color(hsv=[176,(30,70),95])Giving both values for a range isn't required, you can use this to just set the upper or lower limit by setting the other half toRANDOM:random_cyan=Color(hsv=[176,(RANDOM,70),95])random_cyan=Color(hsv=[176,(30,RANDOM),95])Note: Immutability and HashibilityAll instances of colors are immutable, meaning their values can't be changed once they are defined. This means that each instance ofColor()represents a specific color and will always represent that color. If you feel the need to modify a color, this can easily be done as:old_color=Color(rgb=[83,237,229])#  change hue, but not saturation or valuenew_color=Color(hsv=[230,old_color.hsv.s,old_color.hsv.v])All instances of colors are also hashable. They can be safely used as keys fordict()s and can be added toset()to efficiently find unique colors or to test membership.>>>colors={Color(hex='#7fffd4'):'Can be used in dict() keys!'}>>>Color(name='aquamarine')incolorsTrue>>>colors[Color(rgb=[127,255,212])]'Can be used in dict() keys!'As a result of colors being immutable and hashable, colors that represent the sameRGBvalues will always be unambiguously equal to each other. This prevents a lot of bugs that can randomly appear when working with floathsv/hslvalues and removes the inconsistencies in the conversion algorithm that converts betweenrgbandhsv/hsl. An example that demonstrates this:>>>Color(hsl=[236.94,9.29,84.54])==Color(hsl=[240.0,8.86,84.51])TrueThis results inTruebecause both of thesehslvalues map to the samergbvalue(212, 212, 219)and thus represent the same color.Color schemesTheColor()class also provides some convenience functions to work with color schemes. In the future, these would also be used to build color palettes. For now, the corresponding colors from a color scheme for a specific color can be generated like this:fromacrylicimportColor,Schemescyan=Color(rgb=[83,237,229])complementary_color=cyan.scheme(Schemes.COMPLEMENTARY)cyan_triads=cyan.scheme(Schemes.TRIADIC)cyan_shades=cyan.scheme(Schemes.SHADES)Taking inspiration from traditional art where most of these color schemes originated from, these are calculated using theryb(red-yellow-blue) color wheel by default. To use thergb(red-green-blue) color wheel instead you can passin_rgb=Trueto the.scheme()function.For a list of all the available color schemes and their explanations, checkthis page.Example UsecasesCreate a color usingRGB, use its saturation to create a new color, and print its value as a hex string:orange=Color(rgb=[247,177,79])cyan=Color(hsv=[176.5,orange.hsv.s,98])print(cyan.hex)# Output: '#50FAF0'Generating random aesthetically pleasing colors, which for example can be used to color the default profile pictures for users of an appdefaesthetic_color():returnColor(hsl=[RANDOM,(65,RANDOM),(60,75)])(If you have ever tried generating random colors by randomizingrgbvalues, you would know how badly that works)Finding unique colors:test_set=set([Color(rgb=[61,245,245]),Color(hex='#3DF5F5'),Color(hsl=[180,89.8,60]),Color(hsl=[179.8,90.2,60.1])])print(test_set)# Output: {Color(rgb=(61, 245, 245))}The set contains only one color as all those colors map to the samergbvalues.Sorting all the pixels in an image horizontally by hue:fromacrylicimportColorfromPILimportImageorignal_image=Image.open('test.jpg')sorted_img=orignal_image.copy()pixels=orignal_image.load()foryinrange(sorted_image.height):row=[Color(rgb=pixels[n,y])forninrange(sorted_image.width)]sorted_row=sorted(row,key=lambdac:c.hsl.h)forx,cinenumerate(sorted_row):sorted_image.putpixel((x,y),c.rgb)This example also illustrates how easy it is to integrateacrylicwith other libraries and seamlessly switch betweenrgbandhslContributionsAll contributions toacrylicare welcome and appreciated! Ways in which you can contribute are:Report an issue (here)Raise a pull request (here)Request new featuresSpread the word aboutacrylic!LicenseMIT License: Copyright (c) 2020 - 2022 ArshLicense.txt