color.js
1 "use strict"; 2 Object.defineProperty(exports, "__esModule", { value: true }); 3 exports.areEqual = exports.luminanceFrom = exports.dynamicWith = exports.named = exports.rgbWith = exports.htmlWith = exports.Color = void 0; 4 const optional_1 = require("../types/optional"); 5 // endregion 6 // region Constructors 7 // eslint-disable-next-line no-redeclare, @typescript-eslint/no-redeclare 8 exports.Color = { 9 /** 10 * Create new `HTMLColor` from hexadecimal string representation. 11 * 12 * @param hexString - Hexadecimal string representation. 13 */ 14 fromHex(string) { 15 if ((0, optional_1.isNothing)(string)) { 16 return null; 17 } 18 return { 19 $kind: "html", 20 value: string, 21 }; 22 }, 23 /** 24 * Create new `RBGColor` with RGB components and opacity value. 25 * 26 * @param red - Red color value. 27 * @param green - Green color value. 28 * @param blue - Blue color value. 29 * @param alpha - Opacity value. 30 */ 31 fromRGB(red, green, blue, alpha = 1.0) { 32 const newColor = { 33 $kind: "rgb", 34 red: red, 35 green: green, 36 blue: blue, 37 alpha: alpha, 38 }; 39 return newColor; 40 }, 41 /** 42 * Create new named color using the color name. 43 * 44 * @param name - The name of the color. 45 */ 46 named(name) { 47 const newColor = { 48 $kind: "named", 49 name: name, 50 }; 51 return newColor; 52 }, 53 /** 54 * Create new dynamic color with light and dark color variants. 55 * 56 * @param lightColor - The light color variant. 57 * @param lightHighContrastColor - The light hight-contrast color variant. 58 * @param darkColor - The dark color variant. 59 * @param darkHighContrastColor - The dark hight-contrast color variant. 60 */ 61 dynamicWith(lightColor, lightHighContrastColor, darkColor, darkHighContrastColor) { 62 const newColor = { 63 $kind: "dynamic", 64 lightColor: lightColor, 65 lightHighContrastColor: lightHighContrastColor, 66 darkColor: darkColor, 67 darkHighContrastColor: darkHighContrastColor, 68 }; 69 return newColor; 70 }, 71 // endregion 72 // region Properties 73 /** 74 * Get the luminance of the color. 75 * 76 * @param rgbColor - The RGB color to get luminance for. 77 */ 78 luminanceFrom(rgbColor) { 79 // Note: This is lifted from UIColor_Private 80 // Using RGB color components, calculates and returns (0.2126 * r) + (0.7152 * g) + (0.0722 * b). 81 return rgbColor.red * 0.2126 + rgbColor.green * 0.7152 + rgbColor.blue * 0.0722; 82 }, 83 // endregion 84 // region Identity 85 /** 86 * Compare two colors for equality. 87 * 88 * @param color1 - Left hand side color to compare. 89 * @param color2 - Right hand side color to compare. 90 * @returns A Boolean indicating whether the colors are equal. 91 */ 92 areEqual(color1, color2) { 93 if ((0, optional_1.isNothing)(color1)) { 94 return (0, optional_1.isNothing)(color2); 95 } 96 else if ((0, optional_1.isNothing)(color2)) { 97 return (0, optional_1.isNothing)(color1); 98 } 99 const kind1 = color1.$kind; 100 const kind2 = color2.$kind; 101 if (kind1 === "named" && kind2 === "named") { 102 const namedColor1 = color1; 103 const namedColor2 = color2; 104 return namedColor1.name === namedColor2.name; 105 } 106 else if (kind1 === "rgb" && kind2 === "rgb") { 107 const rgbColor1 = color1; 108 const rgbColor2 = color2; 109 return (rgbColor1.red === rgbColor2.red && 110 rgbColor1.green === rgbColor2.green && 111 rgbColor1.blue === rgbColor2.blue && 112 rgbColor1.alpha === rgbColor2.alpha); 113 } 114 else if (kind1 === "dynamic" && kind2 === "dynamic") { 115 const dynamicColor1 = color1; 116 const dynamicColor2 = color2; 117 return (exports.Color.areEqual(dynamicColor1.lightColor, dynamicColor2.lightColor) && 118 exports.Color.areEqual(dynamicColor1.lightHighContrastColor, dynamicColor2.lightHighContrastColor) && 119 exports.Color.areEqual(dynamicColor1.darkColor, dynamicColor2.darkColor) && 120 exports.Color.areEqual(dynamicColor1.darkHighContrastColor, dynamicColor2.darkHighContrastColor)); 121 } 122 else { 123 return false; 124 } 125 }, 126 }; 127 /** 128 * Create new `HTMLColor` from hexadecimal string representation. 129 * 130 * @param hexString - Hexadecimal string representation. 131 * 132 * @deprecated This symbol has been moved to `Color.fromHex` and will be removed 133 * in the future. 134 */ 135 const htmlWith = exports.Color.fromHex; 136 exports.htmlWith = htmlWith; 137 /** 138 * Create new `RBGColor` with RGB components and opacity value. 139 * 140 * @param red - Red color value. 141 * @param green - Green color value. 142 * @param blue - Blue color value. 143 * @param alpha - Opacity value. 144 * 145 * @deprecated This symbol has been moved to `Color.fromRGB` and will be removed 146 * in the future. 147 */ 148 const rgbWith = exports.Color.fromRGB; 149 exports.rgbWith = rgbWith; 150 /** 151 * Create new named color using the color name. 152 * 153 * @param name - The name of the color. 154 * 155 * @deprecated This symbol has been moved to `Color.named` and will be removed 156 * in the future. 157 */ 158 const named = exports.Color.named; 159 exports.named = named; 160 /** 161 * Create new dynamic color with light and dark color variants. 162 * 163 * @param lightColor - The light color variant. 164 * @param lightHighContrastColor - The light hight-contrast color variant. 165 * @param darkColor - The dark color variant. 166 * @param darkHighContrastColor - The dark hight-contrast color variant. 167 * 168 * @deprecated This symbol has been moved to `Color.dynamicWith` and will be removed 169 * in the future. 170 */ 171 const dynamicWith = exports.Color.dynamicWith; 172 exports.dynamicWith = dynamicWith; 173 /** 174 * Get the luminance of the color. 175 * 176 * @param rgbColor - The RGB color to get luminance for. 177 * 178 * @deprecated This symbol has been moved to `Color.luminanceFrom` and will be removed 179 * in the future. 180 */ 181 const luminanceFrom = exports.Color.luminanceFrom; 182 exports.luminanceFrom = luminanceFrom; 183 /** 184 * Compare two colors for equality. 185 * 186 * @param color1 - Left hand side color to compare. 187 * @param color2 - Right hand side color to compare. 188 * @returns A Boolean indicating whether the colors are equal. 189 * 190 * @deprecated This symbol has been moved to `Color.areEqual` and will be removed 191 * in the future. 192 */ 193 const areEqual = exports.Color.areEqual; 194 exports.areEqual = areEqual; 195 // endregion 196 //# sourceMappingURL=color.js.map