Code Monkey home page Code Monkey logo

acrylic's Introduction

logo

PyPI PyPI - Python Version PyPI - License

Have you ever wanted a simple and intuitive way to work with colors in python? Then this library is for you! acrylic is 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, name

Small example:

from acrylic import Color, RANDOM

#  Define a color using rgb values
orange = Color(rgb=[247, 177, 79])

#  Use saturation from that color to create a new random color with hsv
random_color = Color(hsv=[RANDOM, orange.hsv.s, 98])

#  Print the random color's value in hex
print(random_color.hex)  # Output: '#50FAF0'

check out more examples below.

acrylic also has support for color 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 schemes here

How to Install

acrylic can be installed using pip:

pip install acrylic

It has no dependencies and works with Python >=3.6

Documentation

Defining Colors

You can create a new color like this:

from acrylic import Color
cyan = Color(rgb=[83, 237, 229])

The same syntax can be used to give input in any of the supported color formats. Currently supported formats are rgb, hsv, hsl, hex and ryb. 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 for rgb and ryb should be between 0 - 255
  • The value of hue for hsv and hsl should be between 0.0 - 360.0 and the other two components should be between 0.0 - 100.0.
  • Values for hex should be a string representing 6-digit hex number
  • Values for name should be a string representing a valid CSS3 color name

Converting between color formats

Any instance of Color() 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 say rgb to hsl as easy as doing:

hsl_values = Color(rgb=[83, 237, 229]).hsl

Accessing values of colors

When accessing these attributes for a color, it returns the values back as a namedtuple instance. This behaves exactly as a normal tuple would, 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 values
Rgb(r=83, g=237, b=229)
>>> [x for x in cyan.rgb]  # can be iterated over like a normal tuple
[83, 237, 229]
>>> cyan.rgb[1]  # items can be accessed via index
237
>>> 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 color

In 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:

from acrylic import Color, RANDOM
random_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, check example 2 below)

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 value a <= value <= b will 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 to RANDOM:

random_cyan = Color(hsv=[176, (RANDOM, 70), 95])
random_cyan = Color(hsv=[176, (30, RANDOM), 95])

Note: Immutability and Hashibility

  • All instances of colors are immutable, meaning their values can't be changed once they are defined. This means that each instance of Color() 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 value
    new_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 for dict()s and can be added to set() to efficiently find unique colors or to test membership.
    >>> colors = {Color(hex='#7fffd4'): 'Can be used in dict() keys!'}
    >>> Color(name='aquamarine') in colors
    True
    >>> 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 same RGB values will always be unambiguously equal to each other. This prevents a lot of bugs that can randomly appear when working with float hsv/hsl values and removes the inconsistencies in the conversion algorithm that converts between rgb and hsv/hsl. An example that demonstrates this:
    >>> Color(hsl=[236.94, 9.29, 84.54]) == Color(hsl=[240.0, 8.86, 84.51])
    True
    This results in True because both of these hsl values map to the same rgb value (212, 212, 219) and thus represent the same color.

Color schemes

The Color() 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:

from acrylic import Color, Schemes
cyan = 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 the ryb(red-yellow-blue) color wheel by default. To use the rgb(red-green-blue) color wheel instead you can pass in_rgb=True to the .scheme() function.

For a list of all the available color schemes and their explanations, check this page.

Example Usecases

  1. Create a color using RGB, 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'
  2. Generating random aesthetically pleasing colors, which for example can be used to color the default profile pictures for users of an app

    def aesthetic_color():
        return Color(hsl=[RANDOM, (65, RANDOM), (60, 75)])

    (If you have ever tried generating random colors by randomizing rgb values, you would know how badly that works)

  3. 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 same rgb values.

  4. Sorting all the pixels in an image horizontally by hue:

    from acrylic import Color
    from PIL import Image
    
    orignal_image = Image.open('test.jpg')
    sorted_img = orignal_image.copy()
    pixels = orignal_image.load()
    
    for y in range(sorted_image.height):
          row = [Color(rgb=pixels[n, y]) for n in range(sorted_image.width)]
          sorted_row = sorted(row, key=lambda c: c.hsl.h)
          for x, c in enumerate(sorted_row):
              sorted_image.putpixel((x, y), c.rgb)

    This example also illustrates how easy it is to integrate acrylic with other libraries and seamlessly switch between rgb and hsl

Contributions

All contributions to acrylic are welcome and appreciated! Ways in which you can contribute are:

  • Report an issue (here)
  • Raise a pull request (here)
  • Request new features
  • Spread the word about acrylic!

License

MIT License: Copyright (c) 2020 - 2022 Arsh
License.txt

acrylic's People

Contributors

prdx23 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

acrylic's Issues

Complementary colors default to randomized fuzzy

The comment for Schemes.scheme says the default for fuzzy is 0:

fuzzy(int, optional):
adds a random value between -fuzzy and +fuzzy to
the generated hue. Can be set to `RANDOM` to use
the recommended value.
Default: 0

But it's coming in with a default value of -1:

def scheme(self, name, in_rgb=False, fuzzy=-1):

Causing it to randomize the complementary colors returned:

RANDOM = -1

scheme.complementary ryb colors very inaccurate (also resulting format broken)

Hi

I can not get accurate (or, well, "good enough") complementary colors of RYB colors. I tried looking into the code, but couldn't find where that is done.
It is accurate if I'd want the complementary of blue, yellow or red. But it feels like, in the conversion from rgb to ryb or back, there is no mapping done via trilinear interpolation or whatnot. (Interpolation between fixed conversion points on the ryb spectrum) I found that other programs do that. There is a C program I almost thought about porting to python, but I really have no idea what I'm doing.

What I do is:

  • Get tuple rgb color and make it acrylic
  • do a scheme complementary with "in_rgb=False" (and "fuzzy=0" because default is "-1" but there is another issue for that)
  • (find out that the resulting format now requires you to write: "color[0].rgb" instead of "color.rgb")
  • give back rgb code
    colorPx = (53, 53, 53)
    colorPx = acrylic.Color(rgb = (colorPx[0], colorPx[1], colorPx[2]))                        # colorPx = Color(rgb=(53, 53, 53))
    colorPx = colorPx.scheme(acrylic.Schemes.COMPLEMENTARY, in_rgb=False, fuzzy=0)             # colorPx = [Color(hsl=(0.0, 0.0, 20.78))] ... it's now a list with Color inside
    colorPx = acrylic.Color(ryb = (colorPx[0].ryb[0], colorPx[0].ryb[1], colorPx[0].ryb[2]))   # colorPx = Color(ryb=(202, 202, 202))
    colorPx = (colorPx.rgb[0], colorPx.rgb[1], colorPx.rgb[2])                                 # colorPx = (53, 53, 53) (bad example)

(I didn't want to create other variables just for this, so I used the same again and again)

I also tried converting to a ryb tuple after creating a acrylic.color (color.ryb[0], 1 ,2...), then creating another acrylic.color but this time it's "ryb = ...", do the scheme conversions, create another ryb tuple (this time with color[0].ryb[0] , 1 , 2..) Convert it to a rgb tuplr and look at that. But it's of course identical.

I can never get these complementary colors from the ryb example here: https://en.wikipedia.org/wiki/Complementary_colors

Besides that: I feel like the result when I convert to RGB or something else via colorPx = acrylic.Color(ryb = (colorPx[0], colorPx[1], colorPx[2])).rgb giving the result back in the format of Rgb(r=46, g=45, b=42) is very annoying, for me at least. I see the value in having a Rgb I can get r out of via color.Rgb.r, maybe. If that is even the correct way. Sorry, these fundamental things necessary to start programming are beyond me. :) (That's why I'm not doing that up there in the code)
But if I'm not inserting three variables I shouldn't get three variables out. Int in int out. Well... you know what I mean.

Greetings

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.