|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. */ |
| 18 | + |
| 19 | +import { |
| 20 | + stringToRGB, |
| 21 | + validateColor, |
| 22 | + defaultD3Color, |
| 23 | + argsToRGB, |
| 24 | + RgbObject, |
| 25 | + argsToRGBString, |
| 26 | + RGBtoString, |
| 27 | +} from './d3_utils'; |
| 28 | + |
| 29 | +describe('d3 Utils', () => { |
| 30 | + describe('stringToRGB', () => { |
| 31 | + describe('bad colors or undefined', () => { |
| 32 | + it('should return default color for undefined color string', () => { |
| 33 | + expect(stringToRGB()).toMatchObject({ |
| 34 | + r: 255, |
| 35 | + g: 0, |
| 36 | + b: 0, |
| 37 | + opacity: 1, |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should return default RgbObject', () => { |
| 42 | + expect(stringToRGB('not a color')).toMatchObject({ |
| 43 | + r: 255, |
| 44 | + g: 0, |
| 45 | + b: 0, |
| 46 | + opacity: 1, |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return default color if bad opacity', () => { |
| 51 | + expect(stringToRGB('rgba(50,50,50,x)')).toMatchObject({ |
| 52 | + r: 255, |
| 53 | + g: 0, |
| 54 | + b: 0, |
| 55 | + opacity: 1, |
| 56 | + }); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + describe('hex colors', () => { |
| 61 | + it('should return RgbObject', () => { |
| 62 | + expect(stringToRGB('#ef713d')).toMatchObject({ |
| 63 | + r: 239, |
| 64 | + g: 113, |
| 65 | + b: 61, |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should return RgbObject from shorthand', () => { |
| 70 | + expect(stringToRGB('#ccc')).toMatchObject({ |
| 71 | + r: 204, |
| 72 | + g: 204, |
| 73 | + b: 204, |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return RgbObject with correct opacity', () => { |
| 78 | + // https://gist.github.com/lopspower/03fb1cc0ac9f32ef38f4 |
| 79 | + expect(stringToRGB('#ef713d80').opacity).toBeCloseTo(0.5, 1); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should return correct RgbObject for alpha value of 0', () => { |
| 83 | + expect(stringToRGB('#00000000')).toMatchObject({ |
| 84 | + r: 0, |
| 85 | + g: 0, |
| 86 | + b: 0, |
| 87 | + opacity: 0, |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('rgb colors', () => { |
| 93 | + it('should return RgbObject', () => { |
| 94 | + expect(stringToRGB('rgb(50,50,50)')).toMatchObject({ |
| 95 | + r: 50, |
| 96 | + g: 50, |
| 97 | + b: 50, |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should return RgbObject with correct opacity', () => { |
| 102 | + expect(stringToRGB('rgba(50,50,50,0.25)').opacity).toBe(0.25); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should return correct RgbObject for alpha value of 0', () => { |
| 106 | + expect(stringToRGB('rgba(50,50,50,0)')).toMatchObject({ |
| 107 | + r: 50, |
| 108 | + g: 50, |
| 109 | + b: 50, |
| 110 | + opacity: 0, |
| 111 | + }); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('hsl colors', () => { |
| 116 | + it('should return RgbObject', () => { |
| 117 | + expect(stringToRGB('hsl(0,0%,50%)')).toMatchObject({ |
| 118 | + r: 127.5, |
| 119 | + g: 127.5, |
| 120 | + b: 127.5, |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should return RgbObject with correct opacity', () => { |
| 125 | + expect(stringToRGB('hsla(0,0%,50%,0.25)').opacity).toBe(0.25); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should return correct RgbObject for alpha value of 0', () => { |
| 129 | + expect(stringToRGB('hsla(0,0%,50%,0)')).toEqual({ |
| 130 | + r: 127.5, |
| 131 | + g: 127.5, |
| 132 | + b: 127.5, |
| 133 | + opacity: 0, |
| 134 | + }); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('named colors', () => { |
| 139 | + it('should return RgbObject', () => { |
| 140 | + expect(stringToRGB('aquamarine')).toMatchObject({ |
| 141 | + r: 127, |
| 142 | + g: 255, |
| 143 | + b: 212, |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it('should return default RgbObject with 0 opacity', () => { |
| 148 | + expect(stringToRGB('transparent')).toMatchObject({ |
| 149 | + r: 0, |
| 150 | + g: 0, |
| 151 | + b: 0, |
| 152 | + opacity: 0, |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should return default RgbObject with 0 opacity even with override', () => { |
| 157 | + expect(stringToRGB('transparent', 0.5)).toMatchObject({ |
| 158 | + r: 0, |
| 159 | + g: 0, |
| 160 | + b: 0, |
| 161 | + opacity: 0, |
| 162 | + }); |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + describe('Optional opactiy override', () => { |
| 167 | + it('should override opacity from color', () => { |
| 168 | + expect(stringToRGB('rgba(50,50,50,0.25)', 0.75).opacity).toBe(0.75); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should use OpacityFn to compute opacity override', () => { |
| 172 | + expect(stringToRGB('rgba(50,50,50,0.25)', (o) => o * 2).opacity).toBe(0.5); |
| 173 | + }); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + describe('validateColor', () => { |
| 178 | + it.each<string>(['r', 'g', 'b', 'opacity'])('should return null if %s is NaN', (value) => { |
| 179 | + expect( |
| 180 | + validateColor({ |
| 181 | + ...defaultD3Color, |
| 182 | + [value]: NaN, |
| 183 | + }), |
| 184 | + ).toBeNull(); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should return valid colors', () => { |
| 188 | + expect(validateColor(defaultD3Color)).toBe(defaultD3Color); |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + describe('argsToRGB', () => { |
| 193 | + it.each<keyof RgbObject>(['r', 'g', 'b', 'opacity'])('should return defaultD3Color if %s is NaN', (value) => { |
| 194 | + const { r, g, b, opacity }: RgbObject = { |
| 195 | + ...defaultD3Color, |
| 196 | + [value]: NaN, |
| 197 | + }; |
| 198 | + expect(argsToRGB(r, g, b, opacity)).toEqual(defaultD3Color); |
| 199 | + }); |
| 200 | + |
| 201 | + it('should return valid colors', () => { |
| 202 | + const { r, g, b, opacity } = defaultD3Color; |
| 203 | + expect(argsToRGB(r, g, b, opacity)).toEqual(defaultD3Color); |
| 204 | + }); |
| 205 | + }); |
| 206 | + |
| 207 | + describe('argsToRGBString', () => { |
| 208 | + it('should return valid colors', () => { |
| 209 | + const { r, g, b, opacity } = defaultD3Color; |
| 210 | + expect(argsToRGBString(r, g, b, opacity)).toBe('rgb(255, 0, 0)'); |
| 211 | + }); |
| 212 | + }); |
| 213 | + |
| 214 | + describe('RGBtoString', () => { |
| 215 | + it('should return valid colors', () => { |
| 216 | + expect(RGBtoString(defaultD3Color)).toBe('rgb(255, 0, 0)'); |
| 217 | + }); |
| 218 | + }); |
| 219 | +}); |
0 commit comments