# -*- coding: utf-8 -*-
# Two implementations. The first is based on color brightness obtained from:-
# https://www.w3.org/TR/AERT#color-contrast
# The second is based on relative luminance calculation for sRGB obtained from:-
# https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
# and contrast ratio calculation obtained from:-
# https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
#
# Preliminary testing suggests color brightness more closely matches the
# Material Design spec suggested text colors, but the alternative implementation
# is both newer and the current 'correct' recommendation, so is included here
# as an option.


def _color_brightness(color):
    # Implementation of color brightness method
    brightness = color[0] * 299 + color[1] * 587 + color[2] * 114
    brightness = brightness
    return brightness


def _black_or_white_by_color_brightness(color):
    if _color_brightness(color) >= 500:
        return 'black'
    else:
        return 'white'


def _normalized_channel(color):
    # Implementation of contrast ratio and relative luminance method
    if color <= 0.03928:
        return color / 12.92
    else:
        return ((color + 0.055) / 1.055) ** 2.4


def _luminance(color):
    rg = _normalized_channel(color[0])
    gg = _normalized_channel(color[1])
    bg = _normalized_channel(color[2])
    return 0.2126*rg + 0.7152*gg + 0.0722*bg


def _black_or_white_by_contrast_ratio(color):
    l_color = _luminance(color)
    l_black = 0.0
    l_white = 1.0
    b_contrast = (l_color + 0.05) / (l_black + 0.05)
    w_contrast = (l_white + 0.05) / (l_color + 0.05)
    return 'white' if w_contrast >= b_contrast else 'black'


def get_contrast_text_color(color, use_color_brightness=True):
    if use_color_brightness:
        contrast_color = _black_or_white_by_color_brightness(color)
    else:
        contrast_color = _black_or_white_by_contrast_ratio(color)
    if contrast_color == 'white':
        return 1, 1, 1, 1
    else:
        return 0, 0, 0, 1

if __name__ == '__main__':
    from kivy.utils import get_color_from_hex
    from kivymd.color_definitions import colors, text_colors
    for c in ['Red', 'Pink', 'Purple', 'DeepPurple', 'Indigo', 'Blue',
              'LightBlue', 'Cyan', 'Teal', 'Green', 'LightGreen', 'Lime',
              'Yellow', 'Amber', 'Orange', 'DeepOrange', 'Brown', 'Grey',
              'BlueGrey']:
        print("For the {} color palette:".format(c))
        for h in ['50', '100', '200', '300', '400', '500', '600', '700',
                  '800', '900', 'A100', 'A200', 'A400', 'A700']:
            hex_color = colors[c].get(h)
            if hex_color:
                col = get_color_from_hex(hex_color)
                col_bri = get_contrast_text_color(col)
                con_rat = get_contrast_text_color(col, use_color_brightness=False)
                text_color = text_colors[c][h]
                print("   The {} hue gives {} using color brightness, {} using contrast ratio, and {} from the MD spec"
                      .format(h, col_bri, con_rat, text_color))

@@ -2170,240 +2170,82 @@
  in 
-%5B'Red', 'Pink', 'Purple', 'DeepPurple', 'Indigo', 'Blue',%0A              'LightBlue', 'Cyan', 'Teal', 'Green', 'LightGreen', 'Lime',%0A              'Yellow', 'Amber', 'Orange', 'DeepOrange', 'Brown', 'Grey',%0A              'BlueGrey'%5D:
+colors.items():%0A        if c%5B0%5D in %5B'Light', 'Dark'%5D:%0A            continue
 %0A   
@@ -2291,16 +2291,19 @@
 format(c
+%5B0%5D
 ))%0A     
@@ -2313,174 +2313,40 @@
 for 
-h in %5B'50', '100', '200', '300', '400', '500', '600', '700',%0A                  '800', '900', 'A100', 'A200', 'A400', 'A700'%5D:%0A            hex_color = colors%5Bc%5D.get(h)
+name, hex_color in c%5B1%5D.items():
 %0A   
@@ -2604,11 +2604,17 @@
 rs%5Bc
-%5D%5Bh
+%5B0%5D%5D%5Bname
 %5D%0A  
@@ -2761,17 +2761,20 @@
 .format(
-h
+name
 , col_br

remove hard-coded colors and hues