Skip to main content

Material You color generation algorithms in pure python!

Project description

image

Material You color algorithms for python!

It is built in reference with offical typescript implementation except it's color quantization part, which is based on c++ implementation thanks to pybind.

Features

  1. Up to date with material-foundation/material-color-utilities.
  2. Uses official c++ sources for quantization backend, which makes color generation fast!

Minimal running example:

Run file tests/test_all.py as:

python3 test_all.py <image path> <quality>

Maximum quality is 1 that means use all pixels, and quality number more than 1 means how many pixels to skip in between while reading, also you can see it as compression.

Click to view result

Image Used, size was 8MB

image image

Usage

Install

You can easily install it from pip by executing:

pip3 install materialyoucolor --upgrade

Prebuilt binaries are avaliable for linux, windows and macos. If prebuilt binaries aren't available, then you should manually build and install.

Build and install

# Install 
pip3 install https://github.com/T-Dynamos/materialyoucolor-python/archive/master.zip

OS Specific

Arch Linux

yay -S python-materialyoucolor

Thanks :heart: to @midn8hustlr for this AUR package.

Android (using kivy's buildozer)

Ensure these lines in buildozer.spec:

requirements = materialyoucolor
p4a.branch = develop

IOS (using kivy's kivy-ios)

Install latest version of kivy-ios and use as:

toolchain build materialyoucolor

Usage examples

Click to show
  • Generate non dynamic colors
from materialyoucolor.scheme import Scheme
from materialyoucolor.scheme.scheme_android import SchemeAndroid

# Color is a an int, which is made as:
# 0xff + hex_code (without #)
# Eg: 0xff + #4181EE = 0xff4181EE
# To convert hex to this form, do `int("0xff" + "<hex code>", 16)`
color = 0xff4181EE

print(Scheme.light(color).props)
print(Scheme.dark(color).props)
# Props is a dict, key is color name and value is rgba format list
# {'primary': [0, 90, 195, 255], 'onPrimary': ....

# Same way for android
print(SchemeAndroid.light(color).props)
print(SchemeAndroid.dark(color).props)
  • Generate dynamic colors
# Color in hue, chroma, tone form
from materialyoucolor.hct import Hct
from materialyoucolor.dynamiccolor.material_dynamic_colors import MaterialDynamicColors

# There are 9 different variants of scheme.
from materialyoucolor.scheme.scheme_tonal_spot import SchemeTonalSpot
# Others you can import: SchemeExpressive, SchemeFruitSalad, SchemeMonochrome, SchemeRainbow, SchemeVibrant, SchemeNeutral, SchemeFidelity and SchemeContent

# SchemeTonalSpot is android default
scheme = SchemeTonalSpot( # choose any scheme here
    Hct.from_int(0xff4181EE), # source color in hct form
    True, # dark mode
    0.0, # contrast
)

for color in vars(MaterialDynamicColors).keys():
    color_name = getattr(MaterialDynamicColors, color)
    if hasattr(color_name, "get_hct"): # is a color
        print(color, color_name.get_hct(scheme).to_rgba()) # print name of color and value in rgba format

# background [14, 20, 21, 255]
# onBackground [222, 227, 229, 255]
# surface [14, 20, 21, 255]
# surfaceDim [14, 20, 21, 255]
# ...
  • Generate and score colors from image
# Pillow is required to open image to array of pixels
from PIL import Image
# C++ QuantizeCelebi
from materialyoucolor.quantize import QuantizeCelebi
# Material You's default scoring of colors
from materialyoucolor.score.score import Score

# Open image 
image = Image.open("path_to_some_image.jpg")
pixel_len = image.width * image.height
image_data = image.getdata()

# Quality 1 means skip no pixels
quality = 1
pixel_array = [image_data[_] for _ in range(0, pixel_len, quality)]

# Run algorithm
result = QuantizeCelebi(pixel_array, 128) # 128 -> number desired colors, default 128
print(result)
# {4278722365: 2320, 4278723396: 2405, 4278723657: 2366,...
# result is a dict where key is
# color in integer form (which you can convert later), and value is population

print(Score.score(result))
# [4278722365, 4278723657]
# list of selected colors in integer form

FAQ

  1. How it is different from avanisubbiah/material-color-utilities?

See https://github.com/T-Dynamos/materialyoucolor-python/issues/3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

materialyoucolor-2.0.9.tar.gz (249.7 kB view hashes)

Uploaded Source

Built Distributions

materialyoucolor-2.0.9-cp312-cp312-win_amd64.whl (150.6 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

materialyoucolor-2.0.9-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (205.9 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

materialyoucolor-2.0.9-cp312-cp312-macosx_10_9_universal2.whl (336.0 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

materialyoucolor-2.0.9-cp311-cp311-win_amd64.whl (150.4 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

materialyoucolor-2.0.9-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (205.8 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

materialyoucolor-2.0.9-cp311-cp311-macosx_10_9_universal2.whl (340.6 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

materialyoucolor-2.0.9-cp310-cp310-win_amd64.whl (149.3 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

materialyoucolor-2.0.9-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (203.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

materialyoucolor-2.0.9-cp310-cp310-macosx_11_0_x86_64.whl (198.9 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ x86-64

materialyoucolor-2.0.9-cp310-cp310-macosx_10_9_universal2.whl (316.8 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

materialyoucolor-2.0.9-cp39-cp39-win_amd64.whl (149.1 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

materialyoucolor-2.0.9-cp39-cp39-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (204.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.27+ x86-64 manylinux: glibc 2.28+ x86-64

materialyoucolor-2.0.9-cp39-cp39-macosx_11_0_x86_64.whl (199.1 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page