From badec26c69ddc00c6f3164e397785bdf3695d714 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szikszai=20Guszt=C3=A1v?= Date: Sun, 5 May 2024 22:10:59 +0200 Subject: [PATCH 1/2] Updates for the next Mint version. --- source/Color.mint | 466 +++++++++++++++++---------------------- source/ColorPalette.mint | 22 +- source/ColorWheel.mint | 16 +- source/Math.mint | 8 +- tests/Color.mint | 112 +++++----- 5 files changed, 282 insertions(+), 342 deletions(-) diff --git a/source/Color.mint b/source/Color.mint index 511a2ab..3bca12f 100644 --- a/source/Color.mint +++ b/source/Color.mint @@ -1,27 +1,27 @@ -/* Represents a Color. */ -enum Color { - /* [RGBA Color Representation](https://en.wikipedia.org/wiki/RGBA_color_model) */ +// Represents a Color. +type Color { + // [RGBA Color Representation](https://en.wikipedia.org/wiki/RGBA_color_model) RGBA(Number, Number, Number, Number) - /* [HSV Color Representation](https://en.wikipedia.org/wiki/HSL_and_HSV) */ + // [HSV Color Representation](https://en.wikipedia.org/wiki/HSL_and_HSV) HSVA(Number, Number, Number, Number) - /* [HSL Color Representation](https://en.wikipedia.org/wiki/HSL_and_HSV) */ + // [HSL Color Representation](https://en.wikipedia.org/wiki/HSL_and_HSV) HSLA(Number, Number, Number, Number) - /* [HSI Color Representation](https://en.wikipedia.org/wiki/HSL_and_HSV) */ + // [HSI Color Representation](https://en.wikipedia.org/wiki/HSL_and_HSV) HSIA(Number, Number, Number, Number) - /* [HEX Color Representation](https://en.wikipedia.org/wiki/Web_colors) */ + // [HEX Color Representation](https://en.wikipedia.org/wiki/Web_colors) HEX(String) } -/* Functions to create and manipulate colors. */ +// Functions to create and manipulate colors. module Color { - /* A regexp to detect HEX color values. */ + // A regexp to detect HEX color values. const HEX_REGEXP = Regexp.create("^[0-9A-Fa-f]+$") - /* Generates a color from a string. */ + // Generates a color from a string. fun colorOfString (value : String) : String { ` (() => { @@ -57,10 +57,8 @@ module Color { ` as String } - /* - Calculates the color contrast ratio between two colors (1 to 21). - https://www.w3.org/TR/2008/REC-WCAG20-20081211/#visual-audio-contrast-contrast - */ + // Calculates the color contrast ratio between two colors (1 to 21). + // https://www.w3.org/TR/2008/REC-WCAG20-20081211/#visual-audio-contrast-contrast fun contrastRatio (background : Color, text : Color) : Number { let color1 = Color.relativeLuminance(text) @@ -78,13 +76,11 @@ module Color { Math.round(ratio * 100) / 100 } - /* - Creates a color from a HEX string. - - Color.fromHEX("#FFF") - Color.fromHEX("#FFFFFF") - Color.fromHEX("#FFFFFFFF") - */ + // Creates a color from a HEX string. + // + // Color.fromHEX("#FFF") + // Color.fromHEX("#FFFFFF") + // Color.fromHEX("#FFFFFFFF") fun fromHEX (value : String) : Maybe(Color) { let normalized = value @@ -107,101 +103,89 @@ module Color { let blue = splitted[2] or "" - Maybe::Just(Color::HEX("#{red}#{red}#{green}#{green}#{blue}#{blue}FF")) + Maybe.Just(Color.HEX("#{red}#{red}#{green}#{green}#{blue}#{blue}FF")) } - 6 => Maybe::Just(Color::HEX(normalized + "FF")) - 8 => Maybe::Just(Color::HEX(normalized)) - => Maybe::Nothing + 6 => Maybe.Just(Color.HEX(normalized + "FF")) + 8 => Maybe.Just(Color.HEX(normalized)) + => Maybe.Nothing } } } - /* - Creates a color from hue, saturation, value and alpha parts. - - Color.fromHSVA(0, 100, 100, 100) - */ + // Creates a color from hue, saturation, value and alpha parts. + // + // Color.fromHSVA(0, 100, 100, 100) fun fromHSVA ( hue : Number, saturation : Number, value : Number, alpha : Number ) : Color { - Color::HSVA( + Color.HSVA( Math.clamp(hue, 0, 360), Math.clamp(saturation, 0, 100), Math.clamp(value, 0, 100), Math.clamp(alpha, 0, 100)) } - /* - Creates a color from red, green, blue and alpha parts. - - Color.fromRGBA(255, 255, 255, 100) - */ + // Creates a color from red, green, blue and alpha parts. + // + // Color.fromRGBA(255, 255, 255, 100) fun fromRGBA ( red : Number, green : Number, blue : Number, alpha : Number ) : Color { - Color::RGBA( + Color.RGBA( Math.clamp(red, 0, 255), Math.clamp(green, 0, 255), Math.clamp(blue, 0, 255), Math.clamp(alpha, 0, 100)) } - /* - Returns the alpha value of the color. - - Color.getAlpha(Color.fromHSVA(0,100,100,50)) == 50 - */ + // Returns the alpha value of the color. + // + // Color.getAlpha(Color.fromHSVA(0,100,100,50)) == 50 fun getAlpha (color : Color) : Number { - if let Color::HSVA(red, green, blue, alpha) = color { + if let HSVA(red, green, blue, alpha) = color { alpha } else { getAlpha(toHSVA(color)) } } - /* - Returns the brightness of the given color base on the - [W3C formula](https://www.w3.org/TR/AERT/#color-contrast) - - Color.getBrightness(Color.fromRGBA(255,255,255,100)) == 1000 - Color.getBrightness(Color.fromRGBA(0,0,0,100)) == 0 - */ + // Returns the brightness of the given color base on the + // [W3C formula](https://www.w3.org/TR/AERT/#color-contrast). + // + // Color.getBrightness(Color.fromRGBA(255,255,255,100)) == 1000 + // Color.getBrightness(Color.fromRGBA(0,0,0,100)) == 0 fun getBrightness (color : Color) : Number { - if let Color::RGBA(red, green, blue, alpha) = color { + if let RGBA(red, green, blue, alpha) = color { Math.round((red * 299 + green * 587 + blue * 114) / 1000) } else { getBrightness(toRGBA(color)) } } - /* - Returns the hue of the color. - - Color.getHue(Color.fromHSVA(0,100,100,50)) == 0 - */ + // Returns the hue of the color. + // + // Color.getHue(Color.fromHSVA(0,100,100,50)) == 0 fun getHue (color : Color) : Number { - if let Color::HSVA(hue) = color { + if let HSVA(hue) = color { hue } else { getHue(toHSVA(color)) } } - /* - Returns the saturation of the color. - - Color.getSaturation(Color.fromHSVA(0,100,100,50)) == 100 - */ + // Returns the saturation of the color. + // + // Color.getSaturation(Color.fromHSVA(0,100,100,50)) == 100 fun getSaturation (color : Color) : Number { case color { - Color::HSVA(hue, saturation) => + HSVA(hue, saturation) => saturation => @@ -211,49 +195,43 @@ module Color { } } - /* - Returns the value of the color. - - Color.getValue(Color.fromHSVA(0,100,100,50)) == 100 - */ + // Returns the value of the color. + // + // Color.getValue(Color.fromHSVA(0,100,100,50)) == 100 fun getValue (color : Color) : Number { - if let Color::HSVA(hue, saturation, value) = color { + if let HSVA(hue, saturation, value) = color { value } else { getValue(toHSVA(color)) } } - /* - Makes the color lighter using the given ratio. - - color = - Color.fromHSLA(0, 100, 10, 100) - - Color.lighten(color, 10) == Color.fromHSLA(0, 100, 100, 100) - */ + // Makes the color lighter using the given ratio. + // + // color = + // Color.fromHSLA(0, 100, 10, 100) + // + // Color.lighten(color, 10) == Color.fromHSLA(0, 100, 100, 100) fun lighten (color : Color, ratio : Number) : Color { - if let Color::HSLA(hue, saturation, lightness, alpha) = color { + if let Color.HSLA(hue, saturation, lightness, alpha) = color { let newLightness = Math.clamp(lightness + lightness * ratio, 0, 100) - Color::HSLA(hue, saturation, newLightness, alpha) + Color.HSLA(hue, saturation, newLightness, alpha) } else { lighten(toHSLA(color), ratio) } } - /* - Mixes two given colors using the given weight (0..1). - - color1 = - Color.fromRGBA(255, 255, 255, 100) - - color2 = - Color.fromRGBA(0, 0, 0, 100) - - Color.mix(color1, color2, 0.5) == Color.fromRGBA(128, 128, 128, 100) - */ + // Mixes two given colors using the given weight (0..1). + // + // color1 = + // Color.fromRGBA(255, 255, 255, 100) + // + // color2 = + // Color.fromRGBA(0, 0, 0, 100) + // + // Color.mix(color1, color2, 0.5) == Color.fromRGBA(128, 128, 128, 100) fun mix (color1 : Color, color2 : Color, weight : Number) : Color { { let {red1, green1, blue1, alpha1} = @@ -293,11 +271,11 @@ module Color { let alpha = alpha1 * weight + alpha2 * (1 - weight) - Color::RGBA(red, green, blue, alpha) + Color.RGBA(red, green, blue, alpha) } } - /* Generates a random color. */ + // Generates a random color. fun random : Color { ` (() => { @@ -313,30 +291,26 @@ module Color { ` } - /* - Returns the readable text color (white or black) based on - the brightness of the color. - - color = - Color.fromRGBA(255, 255, 255, 100) - - Color.readableTextColor(color) == Color.fromRGBA(0, 0, 0, 100) - */ + // Returns the readable text color (white or black) based on + // the brightness of the color. + // + // color = + // Color.fromRGBA(255, 255, 255, 100) + // + // Color.readableTextColor(color) == Color.fromRGBA(0, 0, 0, 100) fun readableTextColor (color : Color) : Color { let brightness = getBrightness(color) if brightness > 125 { - mix(Color::RGBA(0, 0, 0, 100), color, 0.1) + mix(Color.RGBA(0, 0, 0, 100), color, 0.1) } else { - mix(Color::RGBA(255, 255, 255, 100), color, 0.05) + mix(Color.RGBA(255, 255, 255, 100), color, 0.05) } } - /* - Calculates the relative luminance of a color. - https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef - */ + // Calculates the relative luminance of a color. + // https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef fun relativeLuminance (color : Color) : Number { let item = Color.toRGBATuple(color) @@ -362,128 +336,114 @@ module Color { 0.2126 * r + 0.7152 * g + 0.0722 * b } - /* - Sets the alpha to the given value of the given color. - - color = - Color.fromHSVA(0, 100, 100, 100) - - Color.setAlpha(color, 50) == Color.fromHSVA(0, 100, 100, 50) - */ + // Sets the alpha to the given value of the given color. + // + // color = + // Color.fromHSVA(0, 100, 100, 100) + // + // Color.setAlpha(color, 50) == Color.fromHSVA(0, 100, 100, 50) fun setAlpha (color : Color, alpha : Number) : Color { - if let Color::HSVA(hue, staturation, value, oldAlpha) = color { - Color::HSVA(hue, staturation, value, Math.round(Math.clamp(alpha, 0, 100))) + if let HSVA(hue, staturation, value, oldAlpha) = color { + Color.HSVA(hue, staturation, value, Math.round(Math.clamp(alpha, 0, 100))) } else { setAlpha(toHSVA(color), alpha) } } - /* - Sets the hue to the given value of the given color. - - color = - Color.fromHSVA(0, 100, 100, 100) - - Color.setHue(color, 50) == Color.fromHSVA(50, 100, 100, 100) - */ + // Sets the hue to the given value of the given color. + // + // color = + // Color.fromHSVA(0, 100, 100, 100) + // + // Color.setHue(color, 50) == Color.fromHSVA(50, 100, 100, 100) fun setHue (color : Color, hue : Number) : Color { - if let Color::HSVA(oldHue, saturation, value, alpha) = color { - Color::HSVA(Math.round(Math.clamp(hue, 0, 360)), saturation, value, alpha) + if let HSVA(oldHue, saturation, value, alpha) = color { + Color.HSVA(Math.round(Math.clamp(hue, 0, 360)), saturation, value, alpha) } else { setHue(toHSVA(color), hue) } } - /* - Sets the lightness to the given value of the given color. - - color = - Color.fromHSLA(0, 100, 100, 100) - - Color.setLightness(50, color) == Color.fromHSLA(0, 100, 50, 100) - */ + // Sets the lightness to the given value of the given color. + // + // color = + // Color.fromHSLA(0, 100, 100, 100) + // + // Color.setLightness(50, color) == Color.fromHSLA(0, 100, 50, 100) fun setLightness (color : Color, lightness : Number) : Color { - if let Color::HSLA(hue, saturation, oldLightness, alpha) = color { - Color::HSLA(hue, saturation, Math.round(Math.clamp(lightness, 0, 100)), alpha) + if let HSLA(hue, saturation, oldLightness, alpha) = color { + Color.HSLA(hue, saturation, Math.round(Math.clamp(lightness, 0, 100)), alpha) } else { setValue(toHSLA(color), lightness) } } - /* - Sets the saturation to the given value of the given color. - - color = - Color.fromHSVA(0, 100, 100, 100) - - Color.setSaturation(50, color) == Color.fromHSVA(0, 50, 100, 100) - */ + // Sets the saturation to the given value of the given color. + // + // color = + // Color.fromHSVA(0, 100, 100, 100) + // + // Color.setSaturation(50, color) == Color.fromHSVA(0, 50, 100, 100) fun setSaturation (color : Color, saturation : Number) : Color { - if let Color::HSVA(hue, oldSaturation, value, alpha) = color { - Color::HSVA(hue, Math.round(Math.clamp(saturation, 0, 100)), value, alpha) + if let HSVA(hue, oldSaturation, value, alpha) = color { + Color.HSVA(hue, Math.round(Math.clamp(saturation, 0, 100)), value, alpha) } else { setSaturation(toHSVA(color), saturation) } } - /* - Sets the value to the given value of the given color. - - color = - Color.fromHSVA(0, 100, 100, 100) - - Color.setValue(50, color) == Color.fromHSVA(0, 100, 50, 100) - */ + // Sets the value to the given value of the given color. + // + // color = + // Color.fromHSVA(0, 100, 100, 100) + // + // Color.setValue(50, color) == Color.fromHSVA(0, 100, 50, 100) fun setValue (color : Color, value : Number) : Color { - if let Color::HSVA(hue, saturation, oldValue, alpha) = color { - Color::HSVA(hue, saturation, Math.round(Math.clamp(value, 0, 100)), alpha) + if let HSVA(hue, saturation, oldValue, alpha) = color { + Color.HSVA(hue, saturation, Math.round(Math.clamp(value, 0, 100)), alpha) } else { setValue(toHSVA(color), value) } } - /* Returns the CSS HSLA representation of the color. */ + // Returns the CSS HSLA representation of the color. fun toCSSHSLA (color : Color) : String { - if let Color::HSLA(hue, saturation, lightness, alpha) = color { + if let HSLA(hue, saturation, lightness, alpha) = color { "hsla(#{hue}, #{saturation}%, #{lightness}%, #{Math.round(alpha / 100)})" } else { toCSSHSLA(toHSLA(color)) } } - /* - Converts the given color to the CSS HEX representation. - - color = - Color.fromHex("#FFF") - - Color.toCSSHex(color) == "#FFFFFFFF" - */ + // Converts the given color to the CSS HEX representation. + // + // color = + // Color.fromHex("#FFF") + // + // Color.toCSSHex(color) == "#FFFFFFFF" fun toCSSHex (color : Color) : String { - if let Color::HEX(value) = color { + if let HEX(value) = color { "##{value}" } else { toCSSHex(toHEX(color)) } } - /* - Converts the given color to the CSS RGBA representation. - - color = - Color.fromRGBA(255, 255, 255, 100) - - Color.toCSSRGBA(color) == "rgba(255,255,255,1)" - */ + // Converts the given color to the CSS RGBA representation. + // + // color = + // Color.fromRGBA(255, 255, 255, 100) + // + // Color.toCSSRGBA(color) == "rgba(255,255,255,1)" fun toCSSRGBA (color : Color) : String { - if let Color::RGBA(red, green, blue, alpha) = color { + if let RGBA(red, green, blue, alpha) = color { "rgba(#{red}, #{green}, #{blue}, #{alpha / 100})" } else { toCSSRGBA(toRGBA(color)) } } - /* Returns the CSS Percent RGBA representation of the color. */ + // Returns the CSS Percent RGBA representation of the color. fun toCSSRGBAPercent (color : Color) : String { let {red, green, blue, alpha} = toRGBAPercentTuple(color) @@ -491,17 +451,15 @@ module Color { "rgba(#{red}%, #{green}%, #{blue}%, #{alpha}%)" } - /* - Converts the internal representation of the color to HEX. - - color = - Color.fromHSVA(0, 100, 100, 100) - - Color.toHEX(color) == Color.fromHEX("#FFFFFFFF") - */ + // Converts the internal representation of the color to HEX. + // + // color = + // Color.fromHSVA(0, 100, 100, 100) + // + // Color.toHEX(color) == Color.fromHEX("#FFFFFFFF") fun toHEX (color : Color) : Color { case color { - Color::RGBA(red, green, blue, alpha) => + RGBA(red, green, blue, alpha) => { let redPart = `#{Math.round(red)}.toString(16).padStart(2,'0')` @@ -515,25 +473,23 @@ module Color { let alphaPart = `#{Math.round(alpha * 2.55)}.toString(16).padStart(2,'0')` - Color::HEX(String.toUpperCase("#{redPart}#{greenPart}#{bluePart}#{alphaPart}")) + Color.HEX(String.toUpperCase("#{redPart}#{greenPart}#{bluePart}#{alphaPart}")) } - Color::HEX => color + HEX => color => toHEX(toRGBA(color)) } } - /* - Converts the internal representation of the color to HSIA. - - color = - Color.fromRGBA(255, 255, 255, 100) - - Color.toHSIA(color) == Color.fromHSVA(0, 100, 100, 100) - */ + // Converts the internal representation of the color to HSIA. + // + // color = + // Color.fromRGBA(255, 255, 255, 100) + // + // Color.toHSIA(color) == Color.fromHSVA(0, 100, 100, 100) fun toHSIA (color : Color) : Color { case color { - Color::RGBA(red, green, blue, alpha) => + RGBA(red, green, blue, alpha) => { let normalizedRed = Math.clamp(red, 0, 255) / 255 @@ -549,7 +505,7 @@ module Color { if normalizedRed == normalizedGreen && normalizedGreen == normalizedBlue { - Color::HSIA( + Color.HSIA( Math.round(0), Math.round(0), Math.round(intensity * 100), @@ -561,7 +517,7 @@ module Color { (normalizedRed - normalizedBlue) * (normalizedGreen - normalizedBlue)) / 2 let hueBase = - Math.acos(w) * 180 / Math:PI + Math.acos(w) * 180 / Math.PI let hue = if normalizedBlue > normalizedGreen { @@ -577,7 +533,7 @@ module Color { 1 - ((Array.min([normalizedRed, normalizedGreen, normalizedBlue]) or 0) / intensity) } - Color::HSIA( + Color.HSIA( Math.round(hue), Math.round(saturation * 100), Math.round(intensity * 100), @@ -585,22 +541,20 @@ module Color { } } - Color::HSIA => color + HSIA => color => toHSIA(toRGBA(color)) } } - /* - Converts the internal representation of the color to HSLA. - - color = - Color.fromRGBA(255, 255, 255, 100) - - Color.toHSLA(color) == Color.fromHSLA(0, 100, 100, 100) - */ + // Converts the internal representation of the color to HSLA. + // + // color = + // Color.fromRGBA(255, 255, 255, 100) + // + // Color.toHSLA(color) == Color.fromHSLA(0, 100, 100, 100) fun toHSLA (color : Color) : Color { case color { - Color::RGBA(red, green, blue, alpha) => + RGBA(red, green, blue, alpha) => { let normalizedRed = Math.clamp(red, 0, 255) / 255 @@ -645,45 +599,41 @@ module Color { 60 * ((normalizedBlue - normalizedRed / delta) + 2) } - Color::HSLA( + Color.HSLA( Math.round(hue), Math.round(saturation * 100), Math.round(lightness * 100), Math.round(alpha)) } - Color::HSLA => color + HSLA => color => toHSLA(toRGBA(color)) } } - /* - Returns the given color as a HSLA tuple. - - color = - Color.fromRGBA(255, 255, 255, 100) - - Color.toHSLATuple(color) == {255,255,255,100} - */ + // Returns the given color as a HSLA tuple. + // + // color = + // Color.fromRGBA(255, 255, 255, 100) + // + // Color.toHSLATuple(color) == {255,255,255,100} fun toHSLATuple (color : Color) : Tuple(Number, Number, Number, Number) { - if let Color::HSLA(hue, saturation, lightness, alpha) = color { + if let HSLA(hue, saturation, lightness, alpha) = color { {hue, saturation, lightness, alpha} } else { toHSLATuple(toHSLA(color)) } } - /* - Converts the internal representation of the color to HSVA. - - color = - Color.fromRGBA(255, 255, 255, 100) - - Color.toHSVA(color) == Color.fromHSVA(0, 100, 100, 100) - */ + // Converts the internal representation of the color to HSVA. + // + // color = + // Color.fromRGBA(255, 255, 255, 100) + // + // Color.toHSVA(color) == Color.fromHSVA(0, 100, 100, 100) fun toHSVA (color : Color) : Color { case color { - Color::RGBA(red, green, blue, alpha) => + RGBA(red, green, blue, alpha) => { let normalizedRed = Math.clamp(red, 0, 255) / 255 @@ -728,31 +678,29 @@ module Color { 60 * ((normalizedBlue - normalizedRed / delta) + 2) } - Color::HSVA( + Color.HSVA( Math.round(hue), Math.round(saturation * 100), Math.round(value * 100), Math.round(alpha)) } - Color::HSVA => color + HSVA => color => toHSVA(toRGBA(color)) } } - /* - Converts the internal representation of the color to RGBA. - - color = - Color.fromHSVA(0, 100, 100, 100) - - Color.toRGBA(color) == Color.fromRGBA(255, 255, 255, 100) - */ + // Converts the internal representation of the color to RGBA. + // + // color = + // Color.fromHSVA(0, 100, 100, 100) + // + // Color.toRGBA(color) == Color.fromRGBA(255, 255, 255, 100) fun toRGBA (color : Color) : Color { case color { - Color::RGBA => color + RGBA => color - Color::HEX(value) => + HEX(value) => ` (() => { const alpha = parseInt(#{value}.slice(6,8), 16) || 255 @@ -763,7 +711,7 @@ module Color { })() ` - Color::HSIA(hue, saturation, intensity, alpha) => + HSIA(hue, saturation, intensity, alpha) => { let normalizedSaturation = Math.clamp(saturation, 0, 100) / 100 @@ -809,14 +757,14 @@ module Color { ({0, 0, 0}) } - Color::RGBA( + Color.RGBA( Math.round((red + m) * 255), Math.round((green + m) * 255), Math.round((blue + m) * 255), normalizedAlpha) } - Color::HSLA(hue, saturation, lightness, alpha) => + HSLA(hue, saturation, lightness, alpha) => { let normalizedSaturation = Math.clamp(saturation, 0, 100) / 100 @@ -854,14 +802,14 @@ module Color { ({c, 0, x}) } - Color::RGBA( + Color.RGBA( Math.ceil((red + m) * 255), Math.ceil((green + m) * 255), Math.ceil((blue + m) * 255), normalizedAlpha) } - Color::HSVA(hue, saturation, value, alpha) => + HSVA(hue, saturation, value, alpha) => { let normalizedSaturation = Math.clamp(saturation, 0, 100) / 100 @@ -899,7 +847,7 @@ module Color { ({c, 0, x}) } - Color::RGBA( + Color.RGBA( Math.ceil((red + m) * 255), Math.ceil((green + m) * 255), Math.ceil((blue + m) * 255), @@ -908,9 +856,9 @@ module Color { } } - /* Returns the the color as an Percent RGBA tuple (0..1). */ + // Returns the the color as an Percent RGBA tuple (0..1). fun toRGBAPercentTuple (color : Color) : Tuple(Number, Number, Number, Number) { - if let Color::RGBA(red, green, blue, alpha) = color { + if let RGBA(red, green, blue, alpha) = color { { Math.round((red / 255) * 100), Math.round((green / 255) * 100), @@ -922,16 +870,14 @@ module Color { } } - /* - Returns the given color as an RGBA tuple. - - color = - Color.fromRGBA(255, 255, 255, 100) - - Color.toRGBATuple(color) == {255,255,255,100} - */ + // Returns the given color as an RGBA tuple. + // + // color = + // Color.fromRGBA(255, 255, 255, 100) + // + // Color.toRGBATuple(color) == {255,255,255,100} fun toRGBATuple (color : Color) : Tuple(Number, Number, Number, Number) { - if let Color::RGBA(red, green, blue, alpha) = color { + if let Color.RGBA(red, green, blue, alpha) = color { {red, green, blue, alpha} } else { toRGBATuple(toRGBA(color)) diff --git a/source/ColorPalette.mint b/source/ColorPalette.mint index 767ee0e..c62425a 100644 --- a/source/ColorPalette.mint +++ b/source/ColorPalette.mint @@ -1,14 +1,12 @@ -/* -A shade for an item in a color palette. This consist of a color and a -readable text color of the color. -*/ -record ColorPalette.Shade { +// A shade for an item in a color palette. This consist of a color and a +// readable text color of the color. +type ColorPalette.Shade { color : String, text : String } -/* A color palette for a color. */ -record ColorPalette { +// A color palette for a color. +type ColorPalette { s900 : ColorPalette.Shade, s800 : ColorPalette.Shade, s700 : ColorPalette.Shade, @@ -24,12 +22,10 @@ record ColorPalette { hue : Number } -/* Functions for creating a color palette. */ +// Functions for creating a color palette. module ColorPalette { - /* - Returns a `ColorPalette.Shade` from a color, by calulating the - readable text color for that color. - */ + // Returns a `ColorPalette.Shade` from a color, by calulating the + // readable text color for that color. fun shadeFromColor (color : Color) : ColorPalette.Shade { { text: Color.toCSSRGBA(Color.readableTextColor(color)), @@ -37,7 +33,7 @@ module ColorPalette { } } - /* Creates a color palette from the given color. */ + // Creates a color palette from the given color. fun fromColor ( color : Color, background : Color, diff --git a/source/ColorWheel.mint b/source/ColorWheel.mint index 5f0aed2..a8abc88 100644 --- a/source/ColorWheel.mint +++ b/source/ColorWheel.mint @@ -1,6 +1,6 @@ -/* A module for creating color wheels. */ +// A module for creating color wheels. module ColorWheel { - /* Adjusts the HUE value. */ + // Adjusts the HUE value. fun adjustHue (value : Number) : Number { let newValue = if value < 0 { @@ -12,7 +12,7 @@ module ColorWheel { newValue % 360 } - /* Returns the complementary color wheel of the color. */ + // Returns the complementary color wheel of the color. fun complementary (color : Color) : Array(Color) { [ color, @@ -20,7 +20,7 @@ module ColorWheel { ] } - /* Returns the split-complementary color wheel of the color. */ + // Returns the split-complementary color wheel of the color. fun splitComplementary (color : Color) : Array(Color) { let hue = Color.getHue(color) @@ -33,7 +33,7 @@ module ColorWheel { ] } - /* Returns the analogous color wheel of the color. */ + // Returns the analogous color wheel of the color. fun analogous (color : Color) : Array(Color) { let hue = Color.getHue(color) @@ -49,7 +49,7 @@ module ColorWheel { ] } - /* Returns the triadic color wheel of the color. */ + // Returns the triadic color wheel of the color. fun triadic (color : Color) : Array(Color) { let hue = Color.getHue(color) @@ -61,7 +61,7 @@ module ColorWheel { ] } - /* Returns the tetradic color wheel of the color. */ + // Returns the tetradic color wheel of the color. fun tetradic (color : Color) : Array(Color) { let hue = Color.getHue(color) @@ -74,7 +74,7 @@ module ColorWheel { ] } - /* Returns the monochromatic color wheel of the color. */ + // Returns the monochromatic color wheel of the color. fun monochromatic (color : Color) : Array(Color) { [ Color.setValue(color, 100), diff --git a/source/Math.mint b/source/Math.mint index 4a0ed9c..cd5288b 100644 --- a/source/Math.mint +++ b/source/Math.mint @@ -1,9 +1,7 @@ module Math { - /* - Returns the value of a number rounded to the nearest integer. - - Math.roundTo(0.7523, 2) == 0.75 - */ + // Returns the value of a number rounded to the nearest integer. + // + // Math.roundTo(0.7523, 2) == 0.75 fun roundTo (number : Number, decimals : Number) : Number { `Math.round(#{number} * (10 * #{decimals})) / (10 * #{decimals})` } diff --git a/tests/Color.mint b/tests/Color.mint index 15a934f..2bc5908 100644 --- a/tests/Color.mint +++ b/tests/Color.mint @@ -1,227 +1,227 @@ suite "Color.toHSVA" { test "converts black correctly" { (Color.fromRGBA(0, 0, 0, 0) - |> Color.toHSVA()) == Color::HSVA(0, 0, 0, 0) + |> Color.toHSVA()) == Color.HSVA(0, 0, 0, 0) } test "converts white correctly" { (Color.fromRGBA(255, 255, 255, 0) - |> Color.toHSVA()) == Color::HSVA(0, 0, 100, 0) + |> Color.toHSVA()) == Color.HSVA(0, 0, 100, 0) } test "converts red correctly" { (Color.fromRGBA(255, 0, 0, 0) - |> Color.toHSVA()) == Color::HSVA(0, 100, 100, 0) + |> Color.toHSVA()) == Color.HSVA(0, 100, 100, 0) } test "converts green correctly" { (Color.fromRGBA(0, 255, 0, 0) - |> Color.toHSVA()) == Color::HSVA(120, 100, 100, 0) + |> Color.toHSVA()) == Color.HSVA(120, 100, 100, 0) } test "converts blue correctly" { (Color.fromRGBA(0, 0, 255, 0) - |> Color.toHSVA()) == Color::HSVA(240, 100, 100, 0) + |> Color.toHSVA()) == Color.HSVA(240, 100, 100, 0) } test "converts cyan correctly" { (Color.fromRGBA(0, 255, 255, 0) - |> Color.toHSVA()) == Color::HSVA(180, 100, 100, 0) + |> Color.toHSVA()) == Color.HSVA(180, 100, 100, 0) } test "converts silver correctly" { (Color.fromRGBA(192, 192, 192, 0) - |> Color.toHSVA()) == Color::HSVA(0, 0, 75, 0) + |> Color.toHSVA()) == Color.HSVA(0, 0, 75, 0) } test "converts gray correctly" { (Color.fromRGBA(128, 128, 128, 0) - |> Color.toHSVA()) == Color::HSVA(0, 0, 50, 0) + |> Color.toHSVA()) == Color.HSVA(0, 0, 50, 0) } test "converts maroon correctly" { (Color.fromRGBA(128, 0, 0, 0) - |> Color.toHSVA()) == Color::HSVA(0, 100, 50, 0) + |> Color.toHSVA()) == Color.HSVA(0, 100, 50, 0) } test "converts olive correctly" { (Color.fromRGBA(128, 128, 0, 0) - |> Color.toHSVA()) == Color::HSVA(60, 100, 50, 0) + |> Color.toHSVA()) == Color.HSVA(60, 100, 50, 0) } test "converts dark green correctly" { (Color.fromRGBA(0, 128, 0, 0) - |> Color.toHSVA()) == Color::HSVA(120, 100, 50, 0) + |> Color.toHSVA()) == Color.HSVA(120, 100, 50, 0) } test "converts purple correctly" { (Color.fromRGBA(128, 0, 128, 0) - |> Color.toHSVA()) == Color::HSVA(300, 100, 50, 0) + |> Color.toHSVA()) == Color.HSVA(300, 100, 50, 0) } test "converts teal correctly" { (Color.fromRGBA(0, 128, 128, 0) - |> Color.toHSVA()) == Color::HSVA(180, 100, 50, 0) + |> Color.toHSVA()) == Color.HSVA(180, 100, 50, 0) } test "converts navy correctly" { (Color.fromRGBA(0, 0, 128, 0) - |> Color.toHSVA()) == Color::HSVA(240, 100, 50, 0) + |> Color.toHSVA()) == Color.HSVA(240, 100, 50, 0) } } suite "Color.toRGBA" { test "converts black correctly" { (Color.fromHSVA(0, 0, 0, 0) - |> Color.toRGBA()) == Color::RGBA(0, 0, 0, 0) + |> Color.toRGBA()) == Color.RGBA(0, 0, 0, 0) } test "converts white correctly" { (Color.fromHSVA(0, 0, 100, 0) - |> Color.toRGBA()) == Color::RGBA(255, 255, 255, 0) + |> Color.toRGBA()) == Color.RGBA(255, 255, 255, 0) } test "converts red correctly" { (Color.fromHSVA(0, 100, 100, 0) - |> Color.toRGBA()) == Color::RGBA(255, 0, 0, 0) + |> Color.toRGBA()) == Color.RGBA(255, 0, 0, 0) } test "converts green correctly" { (Color.fromHSVA(120, 100, 100, 0) - |> Color.toRGBA()) == Color::RGBA(0, 255, 0, 0) + |> Color.toRGBA()) == Color.RGBA(0, 255, 0, 0) } test "converts blue correctly" { (Color.fromHSVA(240, 100, 100, 0) - |> Color.toRGBA()) == Color::RGBA(0, 0, 255, 0) + |> Color.toRGBA()) == Color.RGBA(0, 0, 255, 0) } test "converts yelllow correctly" { (Color.fromHSVA(60, 100, 100, 0) - |> Color.toRGBA()) == Color::RGBA(255, 255, 0, 0) + |> Color.toRGBA()) == Color.RGBA(255, 255, 0, 0) } test "converts cyan correctly" { (Color.fromHSVA(180, 100, 100, 0) - |> Color.toRGBA()) == Color::RGBA(0, 255, 255, 0) + |> Color.toRGBA()) == Color.RGBA(0, 255, 255, 0) } test "converts magenta correctly" { (Color.fromHSVA(300, 100, 100, 0) - |> Color.toRGBA()) == Color::RGBA(255, 0, 255, 0) + |> Color.toRGBA()) == Color.RGBA(255, 0, 255, 0) } test "converts silver correctly" { (Color.fromHSVA(0, 0, 75, 0) - |> Color.toRGBA()) == Color::RGBA(192, 192, 192, 0) + |> Color.toRGBA()) == Color.RGBA(192, 192, 192, 0) } test "converts gray correctly" { (Color.fromHSVA(0, 0, 50, 0) - |> Color.toRGBA()) == Color::RGBA(128, 128, 128, 0) + |> Color.toRGBA()) == Color.RGBA(128, 128, 128, 0) } test "converts maroon correctly" { (Color.fromHSVA(0, 100, 50, 0) - |> Color.toRGBA()) == Color::RGBA(128, 0, 0, 0) + |> Color.toRGBA()) == Color.RGBA(128, 0, 0, 0) } test "converts olive correctly" { (Color.fromHSVA(60, 100, 50, 0) - |> Color.toRGBA()) == Color::RGBA(128, 128, 0, 0) + |> Color.toRGBA()) == Color.RGBA(128, 128, 0, 0) } test "converts dark green correctly" { (Color.fromHSVA(120, 100, 50, 0) - |> Color.toRGBA()) == Color::RGBA(0, 128, 0, 0) + |> Color.toRGBA()) == Color.RGBA(0, 128, 0, 0) } test "converts purple correctly" { (Color.fromHSVA(300, 100, 50, 0) - |> Color.toRGBA()) == Color::RGBA(128, 0, 128, 0) + |> Color.toRGBA()) == Color.RGBA(128, 0, 128, 0) } test "converts teal correctly" { (Color.fromHSVA(180, 100, 50, 0) - |> Color.toRGBA()) == Color::RGBA(0, 128, 128, 0) + |> Color.toRGBA()) == Color.RGBA(0, 128, 128, 0) } test "converts navy correctly" { (Color.fromHSVA(240, 100, 50, 0) - |> Color.toRGBA()) == Color::RGBA(0, 0, 128, 0) + |> Color.toRGBA()) == Color.RGBA(0, 0, 128, 0) } } suite "Color.fromHSVA" { test "returns a new color" { - Color.fromHSVA(0, 0, 0, 0) == Color::HSVA(0, 0, 0, 0) + Color.fromHSVA(0, 0, 0, 0) == Color.HSVA(0, 0, 0, 0) } test "clamps colors to lower value" { - Color.fromHSVA(-10, -10, -10, -10) == Color::HSVA(0, 0, 0, 0) + Color.fromHSVA(-10, -10, -10, -10) == Color.HSVA(0, 0, 0, 0) } test "clamps colors to upper value" { - Color.fromHSVA(1000, 1000, 1000, 1000) == Color::HSVA(360, 100, 100, 100) + Color.fromHSVA(1000, 1000, 1000, 1000) == Color.HSVA(360, 100, 100, 100) } } suite "Color.fromRGBA" { test "returns a new color" { - Color.fromRGBA(0, 0, 0, 0) == Color::RGBA(0, 0, 0, 0) + Color.fromRGBA(0, 0, 0, 0) == Color.RGBA(0, 0, 0, 0) } test "clamps colors to lower value" { - Color.fromRGBA(-10, -10, -10, -10) == Color::RGBA(0, 0, 0, 0) + Color.fromRGBA(-10, -10, -10, -10) == Color.RGBA(0, 0, 0, 0) } test "clamps colors to upper value" { - Color.fromRGBA(1000, 1000, 1000, 1000) == Color::RGBA(255, 255, 255, 100) + Color.fromRGBA(1000, 1000, 1000, 1000) == Color.RGBA(255, 255, 255, 100) } } suite "Color.fromHEX" { test "handles 3 character colors (all same)" { - Color.fromHEX("#000") == Maybe::Just(Color::HEX("000000FF")) + Color.fromHEX("#000") == Maybe.Just(Color.HEX("000000FF")) } test "handles 3 character colors (all different)" { - Color.fromHEX("#FCD") == Maybe::Just(Color::HEX("FFCCDDFF")) + Color.fromHEX("#FCD") == Maybe.Just(Color.HEX("FFCCDDFF")) } test "handles 6 character colors" { - Color.fromHEX("#00FFCC") == Maybe::Just(Color::HEX("00FFCCFF")) + Color.fromHEX("#00FFCC") == Maybe.Just(Color.HEX("00FFCCFF")) } test "handles 8 character colors" { - Color.fromHEX("#00FFCCDD") == Maybe::Just(Color::HEX("00FFCCDD")) + Color.fromHEX("#00FFCCDD") == Maybe.Just(Color.HEX("00FFCCDD")) } test "handles 3 character colors without hashtag" { - Color.fromHEX("000") == Maybe::Just(Color::HEX("000000FF")) + Color.fromHEX("000") == Maybe.Just(Color.HEX("000000FF")) } test "handles 6 character colors without hashtag" { - Color.fromHEX("00FFCC") == Maybe::Just(Color::HEX("00FFCCFF")) + Color.fromHEX("00FFCC") == Maybe.Just(Color.HEX("00FFCCFF")) } test "handles 8 character colors without hashtag" { - Color.fromHEX("00FFCCDD") == Maybe::Just(Color::HEX("00FFCCDD")) + Color.fromHEX("00FFCCDD") == Maybe.Just(Color.HEX("00FFCCDD")) } test "handles 8 character colors lowercase" { - Color.fromHEX("#00ffccdd") == Maybe::Just(Color::HEX("00FFCCDD")) + Color.fromHEX("#00ffccdd") == Maybe.Just(Color.HEX("00FFCCDD")) } test "returns nothing for invalid input" { - Color.fromHEX("#00f!ccdd") == Maybe::Nothing + Color.fromHEX("#00f!ccdd") == Maybe.Nothing } } suite "Color.readableTextColor" { test "returns white for black" { (Color.fromRGBA(0, 0, 0, 100) - |> Color.readableTextColor()) == Color::RGBA(242.25, 242.25, 242.25, 100) + |> Color.readableTextColor()) == Color.RGBA(242.25, 242.25, 242.25, 100) } } @@ -229,16 +229,16 @@ suite "Color.toHSIA" { test "it converts correctly" { let tests = [ - {Color::RGBA(255, 255, 255, 100), Color::HSIA(0, 0, 100, 100)}, - {Color::RGBA(128, 128, 128, 100), Color::HSIA(0, 0, 50, 100)}, - {Color::RGBA(0, 0, 0, 100), Color::HSIA(0, 0, 0, 100)}, - {Color::RGBA(252, 0, 0, 100), Color::HSIA(0, 100, 33, 100)}, - {Color::RGBA(191, 191, 0, 100), Color::HSIA(60, 100, 50, 100)}, - {Color::RGBA(0, 122, 0, 100), Color::HSIA(120, 100, 16, 100)}, - {Color::RGBA(127, 254, 254, 100), Color::HSIA(180, 40, 83, 100)}, - {Color::RGBA(126, 126, 252, 100), Color::HSIA(240, 25, 66, 100)}, - {Color::RGBA(190, 64, 190, 100), Color::HSIA(300, 57, 58, 100)}, - {Color::RGBA(161, 163, 36, 100), Color::HSIA(61, 70, 47, 100)} + {Color.RGBA(255, 255, 255, 100), Color.HSIA(0, 0, 100, 100)}, + {Color.RGBA(128, 128, 128, 100), Color.HSIA(0, 0, 50, 100)}, + {Color.RGBA(0, 0, 0, 100), Color.HSIA(0, 0, 0, 100)}, + {Color.RGBA(252, 0, 0, 100), Color.HSIA(0, 100, 33, 100)}, + {Color.RGBA(191, 191, 0, 100), Color.HSIA(60, 100, 50, 100)}, + {Color.RGBA(0, 122, 0, 100), Color.HSIA(120, 100, 16, 100)}, + {Color.RGBA(127, 254, 254, 100), Color.HSIA(180, 40, 83, 100)}, + {Color.RGBA(126, 126, 252, 100), Color.HSIA(240, 25, 66, 100)}, + {Color.RGBA(190, 64, 190, 100), Color.HSIA(300, 57, 58, 100)}, + {Color.RGBA(161, 163, 36, 100), Color.HSIA(61, 70, 47, 100)} ] let expected = From 81a4eba5ab6a7903772821beac34ecabe28a2a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szikszai=20Guszt=C3=A1v?= Date: Fri, 25 Oct 2024 11:56:16 +0200 Subject: [PATCH 2/2] Changes for the next release. --- .github/workflows/ci.yml | 2 +- .tool-versions | 2 +- LICENSE | 222 ++++------------------------------- mint.json | 2 +- source/Color.mint | 85 +++++--------- source/ColorWheel.mint | 5 +- source/Math.mint | 8 -- tests/Color.mint | 243 +++++++++------------------------------ 8 files changed, 112 insertions(+), 457 deletions(-) delete mode 100644 source/Math.mint diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1e86859..9072348 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -17,7 +17,7 @@ jobs: - name: Setup Mint uses: fabasoad/setup-mint-action@v1.0.15 with: - version: 0.19.0 + version: 0.20.0 - name: Run script run: mint test diff --git a/.tool-versions b/.tool-versions index 0d48765..cc1b266 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -mint 0.19.0 +mint 0.20.0 diff --git a/LICENSE b/LICENSE index 8220540..4b7d44d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,201 +1,21 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [2020] [Mint Language] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. +MIT License + +Copyright (c) 2018 Szikszai Gusztáv + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/mint.json b/mint.json index 537e81e..4514917 100644 --- a/mint.json +++ b/mint.json @@ -1,6 +1,6 @@ { "name": "mint-color", - "mint-version": "0.19.0 <= v < 1.0.0", + "mint-version": "0.20.0 <= v < 1.0.0", "source-directories": [ "source" ], diff --git a/source/Color.mint b/source/Color.mint index 3bca12f..052f124 100644 --- a/source/Color.mint +++ b/source/Color.mint @@ -103,7 +103,8 @@ module Color { let blue = splitted[2] or "" - Maybe.Just(Color.HEX("#{red}#{red}#{green}#{green}#{blue}#{blue}FF")) + Maybe.Just( + Color.HEX("#{red}#{red}#{green}#{green}#{blue}#{blue}FF")) } 6 => Maybe.Just(Color.HEX(normalized + "FF")) @@ -122,11 +123,8 @@ module Color { value : Number, alpha : Number ) : Color { - Color.HSVA( - Math.clamp(hue, 0, 360), - Math.clamp(saturation, 0, 100), - Math.clamp(value, 0, 100), - Math.clamp(alpha, 0, 100)) + Color.HSVA(Math.clamp(hue, 0, 360), Math.clamp(saturation, 0, 100), + Math.clamp(value, 0, 100), Math.clamp(alpha, 0, 100)) } // Creates a color from red, green, blue and alpha parts. @@ -138,11 +136,8 @@ module Color { blue : Number, alpha : Number ) : Color { - Color.RGBA( - Math.clamp(red, 0, 255), - Math.clamp(green, 0, 255), - Math.clamp(blue, 0, 255), - Math.clamp(alpha, 0, 100)) + Color.RGBA(Math.clamp(red, 0, 255), Math.clamp(green, 0, 255), + Math.clamp(blue, 0, 255), Math.clamp(alpha, 0, 100)) } // Returns the alpha value of the color. @@ -185,8 +180,7 @@ module Color { // Color.getSaturation(Color.fromHSVA(0,100,100,50)) == 100 fun getSaturation (color : Color) : Number { case color { - HSVA(hue, saturation) => - saturation + HSVA(hue, saturation) => saturation => color @@ -372,7 +366,8 @@ module Color { // Color.setLightness(50, color) == Color.fromHSLA(0, 100, 50, 100) fun setLightness (color : Color, lightness : Number) : Color { if let HSLA(hue, saturation, oldLightness, alpha) = color { - Color.HSLA(hue, saturation, Math.round(Math.clamp(lightness, 0, 100)), alpha) + Color.HSLA(hue, saturation, Math.round(Math.clamp(lightness, 0, 100)), + alpha) } else { setValue(toHSLA(color), lightness) } @@ -473,7 +468,8 @@ module Color { let alphaPart = `#{Math.round(alpha * 2.55)}.toString(16).padStart(2,'0')` - Color.HEX(String.toUpperCase("#{redPart}#{greenPart}#{bluePart}#{alphaPart}")) + Color.HEX( + String.toUpperCase("#{redPart}#{greenPart}#{bluePart}#{alphaPart}")) } HEX => color @@ -503,18 +499,13 @@ module Color { let intensity = (normalizedRed + normalizedGreen + normalizedBlue) / 3 - if normalizedRed == normalizedGreen && - normalizedGreen == normalizedBlue { - Color.HSIA( - Math.round(0), - Math.round(0), - Math.round(intensity * 100), - Math.round(alpha)) + if normalizedRed == normalizedGreen && normalizedGreen == normalizedBlue { + Color.HSIA(Math.round(0), Math.round(0), + Math.round(intensity * 100), Math.round(alpha)) } else { let w = (normalizedRed - normalizedGreen + normalizedRed - normalizedBlue) / Math.sqrt( - (normalizedRed - normalizedGreen) * (normalizedRed - normalizedGreen) + - (normalizedRed - normalizedBlue) * (normalizedGreen - normalizedBlue)) / 2 + (normalizedRed - normalizedGreen) * (normalizedRed - normalizedGreen) + (normalizedRed - normalizedBlue) * (normalizedGreen - normalizedBlue)) / 2 let hueBase = Math.acos(w) * 180 / Math.PI @@ -530,14 +521,12 @@ module Color { if intensity == 0 { 0 } else { - 1 - ((Array.min([normalizedRed, normalizedGreen, normalizedBlue]) or 0) / intensity) + 1 - ((Array.min( + [normalizedRed, normalizedGreen, normalizedBlue]) or 0) / intensity) } - Color.HSIA( - Math.round(hue), - Math.round(saturation * 100), - Math.round(intensity * 100), - Math.round(alpha)) + Color.HSIA(Math.round(hue), Math.round(saturation * 100), + Math.round(intensity * 100), Math.round(alpha)) } } @@ -599,11 +588,8 @@ module Color { 60 * ((normalizedBlue - normalizedRed / delta) + 2) } - Color.HSLA( - Math.round(hue), - Math.round(saturation * 100), - Math.round(lightness * 100), - Math.round(alpha)) + Color.HSLA(Math.round(hue), Math.round(saturation * 100), + Math.round(lightness * 100), Math.round(alpha)) } HSLA => color @@ -678,11 +664,8 @@ module Color { 60 * ((normalizedBlue - normalizedRed / delta) + 2) } - Color.HSVA( - Math.round(hue), - Math.round(saturation * 100), - Math.round(value * 100), - Math.round(alpha)) + Color.HSVA(Math.round(hue), Math.round(saturation * 100), + Math.round(value * 100), Math.round(alpha)) } HSVA => color @@ -757,11 +740,9 @@ module Color { ({0, 0, 0}) } - Color.RGBA( - Math.round((red + m) * 255), - Math.round((green + m) * 255), - Math.round((blue + m) * 255), - normalizedAlpha) + Color.RGBA(Math.round((red + m) * 255), + Math.round((green + m) * 255), Math.round((blue + m) * 255), + normalizedAlpha) } HSLA(hue, saturation, lightness, alpha) => @@ -802,11 +783,8 @@ module Color { ({c, 0, x}) } - Color.RGBA( - Math.ceil((red + m) * 255), - Math.ceil((green + m) * 255), - Math.ceil((blue + m) * 255), - normalizedAlpha) + Color.RGBA(Math.ceil((red + m) * 255), Math.ceil((green + m) * 255), + Math.ceil((blue + m) * 255), normalizedAlpha) } HSVA(hue, saturation, value, alpha) => @@ -847,11 +825,8 @@ module Color { ({c, 0, x}) } - Color.RGBA( - Math.ceil((red + m) * 255), - Math.ceil((green + m) * 255), - Math.ceil((blue + m) * 255), - normalizedAlpha) + Color.RGBA(Math.ceil((red + m) * 255), Math.ceil((green + m) * 255), + Math.ceil((blue + m) * 255), normalizedAlpha) } } } diff --git a/source/ColorWheel.mint b/source/ColorWheel.mint index a8abc88..270da06 100644 --- a/source/ColorWheel.mint +++ b/source/ColorWheel.mint @@ -14,10 +14,7 @@ module ColorWheel { // Returns the complementary color wheel of the color. fun complementary (color : Color) : Array(Color) { - [ - color, - Color.setHue(color, adjustHue(Color.getHue(color) + 180)) - ] + [color, Color.setHue(color, adjustHue(Color.getHue(color) + 180))] } // Returns the split-complementary color wheel of the color. diff --git a/source/Math.mint b/source/Math.mint deleted file mode 100644 index cd5288b..0000000 --- a/source/Math.mint +++ /dev/null @@ -1,8 +0,0 @@ -module Math { - // Returns the value of a number rounded to the nearest integer. - // - // Math.roundTo(0.7523, 2) == 0.75 - fun roundTo (number : Number, decimals : Number) : Number { - `Math.round(#{number} * (10 * #{decimals})) / (10 * #{decimals})` - } -} diff --git a/tests/Color.mint b/tests/Color.mint index 2bc5908..8d3e8b0 100644 --- a/tests/Color.mint +++ b/tests/Color.mint @@ -1,154 +1,66 @@ -suite "Color.toHSVA" { - test "converts black correctly" { - (Color.fromRGBA(0, 0, 0, 0) - |> Color.toHSVA()) == Color.HSVA(0, 0, 0, 0) - } - - test "converts white correctly" { - (Color.fromRGBA(255, 255, 255, 0) - |> Color.toHSVA()) == Color.HSVA(0, 0, 100, 0) - } - - test "converts red correctly" { - (Color.fromRGBA(255, 0, 0, 0) - |> Color.toHSVA()) == Color.HSVA(0, 100, 100, 0) - } - - test "converts green correctly" { - (Color.fromRGBA(0, 255, 0, 0) - |> Color.toHSVA()) == Color.HSVA(120, 100, 100, 0) - } - - test "converts blue correctly" { - (Color.fromRGBA(0, 0, 255, 0) - |> Color.toHSVA()) == Color.HSVA(240, 100, 100, 0) - } - - test "converts cyan correctly" { - (Color.fromRGBA(0, 255, 255, 0) - |> Color.toHSVA()) == Color.HSVA(180, 100, 100, 0) - } - - test "converts silver correctly" { - (Color.fromRGBA(192, 192, 192, 0) - |> Color.toHSVA()) == Color.HSVA(0, 0, 75, 0) - } - - test "converts gray correctly" { - (Color.fromRGBA(128, 128, 128, 0) - |> Color.toHSVA()) == Color.HSVA(0, 0, 50, 0) - } - - test "converts maroon correctly" { - (Color.fromRGBA(128, 0, 0, 0) - |> Color.toHSVA()) == Color.HSVA(0, 100, 50, 0) - } - - test "converts olive correctly" { - (Color.fromRGBA(128, 128, 0, 0) - |> Color.toHSVA()) == Color.HSVA(60, 100, 50, 0) - } - - test "converts dark green correctly" { - (Color.fromRGBA(0, 128, 0, 0) - |> Color.toHSVA()) == Color.HSVA(120, 100, 50, 0) - } - - test "converts purple correctly" { - (Color.fromRGBA(128, 0, 128, 0) - |> Color.toHSVA()) == Color.HSVA(300, 100, 50, 0) - } - - test "converts teal correctly" { - (Color.fromRGBA(0, 128, 128, 0) - |> Color.toHSVA()) == Color.HSVA(180, 100, 50, 0) - } - - test "converts navy correctly" { - (Color.fromRGBA(0, 0, 128, 0) - |> Color.toHSVA()) == Color.HSVA(240, 100, 50, 0) - } -} - -suite "Color.toRGBA" { - test "converts black correctly" { - (Color.fromHSVA(0, 0, 0, 0) - |> Color.toRGBA()) == Color.RGBA(0, 0, 0, 0) - } - - test "converts white correctly" { - (Color.fromHSVA(0, 0, 100, 0) - |> Color.toRGBA()) == Color.RGBA(255, 255, 255, 0) - } - - test "converts red correctly" { - (Color.fromHSVA(0, 100, 100, 0) - |> Color.toRGBA()) == Color.RGBA(255, 0, 0, 0) - } - - test "converts green correctly" { - (Color.fromHSVA(120, 100, 100, 0) - |> Color.toRGBA()) == Color.RGBA(0, 255, 0, 0) - } - - test "converts blue correctly" { - (Color.fromHSVA(240, 100, 100, 0) - |> Color.toRGBA()) == Color.RGBA(0, 0, 255, 0) - } - - test "converts yelllow correctly" { - (Color.fromHSVA(60, 100, 100, 0) - |> Color.toRGBA()) == Color.RGBA(255, 255, 0, 0) - } - - test "converts cyan correctly" { - (Color.fromHSVA(180, 100, 100, 0) - |> Color.toRGBA()) == Color.RGBA(0, 255, 255, 0) - } - - test "converts magenta correctly" { - (Color.fromHSVA(300, 100, 100, 0) - |> Color.toRGBA()) == Color.RGBA(255, 0, 255, 0) - } - - test "converts silver correctly" { - (Color.fromHSVA(0, 0, 75, 0) - |> Color.toRGBA()) == Color.RGBA(192, 192, 192, 0) - } - - test "converts gray correctly" { - (Color.fromHSVA(0, 0, 50, 0) - |> Color.toRGBA()) == Color.RGBA(128, 128, 128, 0) - } - - test "converts maroon correctly" { - (Color.fromHSVA(0, 100, 50, 0) - |> Color.toRGBA()) == Color.RGBA(128, 0, 0, 0) - } +suite "Conversion Tests" { + fun conversionTest ( + from : Function(Color, Color), + to : Function(Color, Color), + tests : Array(Tuple(Color, Color)) + ) { + let expected = + for item of tests { + item[1] + } - test "converts olive correctly" { - (Color.fromHSVA(60, 100, 50, 0) - |> Color.toRGBA()) == Color.RGBA(128, 128, 0, 0) - } + let expected1 = + for item of tests { + item[0] + } - test "converts dark green correctly" { - (Color.fromHSVA(120, 100, 50, 0) - |> Color.toRGBA()) == Color.RGBA(0, 128, 0, 0) - } + let actual = + for item of tests { + to(item[0]) + } - test "converts purple correctly" { - (Color.fromHSVA(300, 100, 50, 0) - |> Color.toRGBA()) == Color.RGBA(128, 0, 128, 0) - } + let actual1 = + for item of tests { + from(item[1]) + } - test "converts teal correctly" { - (Color.fromHSVA(180, 100, 50, 0) - |> Color.toRGBA()) == Color.RGBA(0, 128, 128, 0) + actual == expected && actual1 == expected1 } - test "converts navy correctly" { - (Color.fromHSVA(240, 100, 50, 0) - |> Color.toRGBA()) == Color.RGBA(0, 0, 128, 0) + test "RGBA <-> HSVA" { + conversionTest(Color.toRGBA, Color.toHSVA, + [ + {Color.RGBA(0, 0, 0, 0), Color.HSVA(0, 0, 0, 0)}, + {Color.RGBA(255, 255, 255, 0), Color.HSVA(0, 0, 100, 0)}, + {Color.RGBA(255, 0, 0, 0), Color.HSVA(0, 100, 100, 0)}, + {Color.RGBA(0, 255, 0, 0), Color.HSVA(120, 100, 100, 0)}, + {Color.RGBA(0, 0, 255, 0), Color.HSVA(240, 100, 100, 0)}, + {Color.RGBA(0, 255, 255, 0), Color.HSVA(180, 100, 100, 0)}, + {Color.RGBA(192, 192, 192, 0), Color.HSVA(0, 0, 75, 0)}, + {Color.RGBA(128, 128, 128, 0), Color.HSVA(0, 0, 50, 0)}, + {Color.RGBA(128, 0, 0, 0), Color.HSVA(0, 100, 50, 0)}, + {Color.RGBA(128, 128, 0, 0), Color.HSVA(60, 100, 50, 0)}, + {Color.RGBA(0, 128, 0, 0), Color.HSVA(120, 100, 50, 0)}, + {Color.RGBA(128, 0, 128, 0), Color.HSVA(300, 100, 50, 0)}, + {Color.RGBA(0, 128, 128, 0), Color.HSVA(180, 100, 50, 0)}, + {Color.RGBA(0, 0, 128, 0), Color.HSVA(240, 100, 50, 0)} + ]) + } + + test "RGBA <-> HSIA" { + conversionTest(Color.toRGBA, Color.toHSIA, + [ + {Color.RGBA(255, 255, 255, 100), Color.HSIA(0, 0, 100, 100)}, + {Color.RGBA(128, 128, 128, 100), Color.HSIA(0, 0, 50, 100)}, + {Color.RGBA(0, 0, 0, 100), Color.HSIA(0, 0, 0, 100)}, + {Color.RGBA(252, 0, 0, 100), Color.HSIA(0, 100, 33, 100)}, + {Color.RGBA(191, 191, 0, 100), Color.HSIA(60, 100, 50, 100)}, + {Color.RGBA(0, 122, 0, 100), Color.HSIA(120, 100, 16, 100)}, + {Color.RGBA(127, 254, 254, 100), Color.HSIA(180, 40, 83, 100)}, + {Color.RGBA(126, 126, 252, 100), Color.HSIA(240, 25, 66, 100)}, + {Color.RGBA(190, 64, 190, 100), Color.HSIA(300, 57, 58, 100)}, + {Color.RGBA(161, 163, 36, 100), Color.HSIA(61, 70, 47, 100)} + ]) } } @@ -224,44 +136,3 @@ suite "Color.readableTextColor" { |> Color.readableTextColor()) == Color.RGBA(242.25, 242.25, 242.25, 100) } } - -suite "Color.toHSIA" { - test "it converts correctly" { - let tests = - [ - {Color.RGBA(255, 255, 255, 100), Color.HSIA(0, 0, 100, 100)}, - {Color.RGBA(128, 128, 128, 100), Color.HSIA(0, 0, 50, 100)}, - {Color.RGBA(0, 0, 0, 100), Color.HSIA(0, 0, 0, 100)}, - {Color.RGBA(252, 0, 0, 100), Color.HSIA(0, 100, 33, 100)}, - {Color.RGBA(191, 191, 0, 100), Color.HSIA(60, 100, 50, 100)}, - {Color.RGBA(0, 122, 0, 100), Color.HSIA(120, 100, 16, 100)}, - {Color.RGBA(127, 254, 254, 100), Color.HSIA(180, 40, 83, 100)}, - {Color.RGBA(126, 126, 252, 100), Color.HSIA(240, 25, 66, 100)}, - {Color.RGBA(190, 64, 190, 100), Color.HSIA(300, 57, 58, 100)}, - {Color.RGBA(161, 163, 36, 100), Color.HSIA(61, 70, 47, 100)} - ] - - let expected = - for item of tests { - item[1] - } - - let expected1 = - for item of tests { - item[0] - } - - let actual = - for item of tests { - Color.toHSIA(item[0]) - } - - let actual1 = - for item of tests { - Color.toRGBA(item[1]) - } - - actual == expected && - actual1 == expected1 - } -}