Skip to content

Commit 4f9c799

Browse files
committed
fix pfe-avatar in ie11
1 parent c29232f commit 4f9c799

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

elements/pfe-avatar/demo/index.html

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.2/vue.min.js"></script>
1414

1515
<!-- uncomment the es5-adapter if you're using the umd version -->
16-
<!-- <script src="/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script> -->
17-
<!-- <script src="/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script> -->
18-
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.min.js"></script> -->
19-
<!-- <script>require(['../../pfelement/pfelement.umd.js', '../pfe-avatar.umd.js'], function(PFElement) { -->
20-
<!-- PFElement.debugLog(true); -->
21-
<!-- })</script> -->
16+
<script src="/@webcomponents/webcomponentsjs/custom-elements-es5-adapter.js"></script>
17+
<script src="/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
18+
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.min.js"></script>
19+
<script>require(['../pfe-avatar.umd.js'])</script>
2220

23-
<script type="module" src="../pfe-avatar.js"></script>
21+
<!-- <script type="module" src="../pfe-avatar.js"></script> -->
2422

2523
<style type="text/css" media="screen">
2624
/* :root {
@@ -47,7 +45,7 @@
4745
</ul>
4846

4947
<script>
50-
const app = new Vue({
48+
var app = new Vue({
5149
el: "#app",
5250
data: {
5351
people: [

elements/pfe-avatar/src/hslrgb.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,31 @@ export function hsl2rgb(_H, _S, _L) {
4848
}
4949

5050
/**
51-
* Convert an RGBcolor to HSL .
51+
* Converts a decimal number into a hexadecimal number, left-padded to a given length.
52+
*/
53+
function toHex(d, len) {
54+
return ("0" + Math.round(d).toString(16)).slice(-len);
55+
}
56+
57+
/**
58+
* Converts an HSL color to a hexadecimal color.
59+
*/
60+
export function hsl2hex(_H, _S, _L) {
61+
return rgb2hex(hsl2rgb(_H, _S, _L));
62+
}
63+
64+
/**
65+
* Converts an RGB color to a hexadecimal color.
66+
*/
67+
export function rgb2hex(_R, _G, _B) {
68+
console.log(`converting ${_R}, ${_G}, ${_B} to hex`);
69+
const ret = "#" + [_R, _G, _B].map(c => toHex(c, 2)).join("");
70+
console.log(`done ${ret}`);
71+
return ret;
72+
}
73+
74+
/**
75+
* Convert an RGBcolor to HSL.
5276
*
5377
* @param {Number} R the red component
5478
* @param {Number} G the green component

elements/pfe-avatar/src/pfe-avatar.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import PFElement from "../pfelement/pfelement.js";
22
import { hash } from "./djb-hash.js";
3-
import { hsl2rgb, rgb2hsl } from "./hslrgb.js";
3+
import { hsl2rgb, rgb2hsl, rgb2hex } from "./hslrgb.js";
44

55
class PfeAvatar extends PFElement {
66
static get tag() {
@@ -26,6 +26,10 @@ class PfeAvatar extends PFElement {
2626
};
2727
}
2828

29+
static get defaultSize() {
30+
return 128;
31+
}
32+
2933
static get defaultColors() {
3034
return "#67accf #448087 #709c6b #a35252 #826cbb";
3135
}
@@ -90,7 +94,9 @@ class PfeAvatar extends PFElement {
9094

9195
_initCanvas() {
9296
this._canvas = this.shadowRoot.querySelector("canvas");
93-
const size = this.var("--pfe-avatar--width").replace(/px$/, "");
97+
const size =
98+
this.var("--pfe-avatar--width").replace(/px$/, "") ||
99+
PfeAvatar.defaultSize;
94100
this._canvas.width = size;
95101
this._canvas.height = size;
96102

@@ -123,7 +129,9 @@ class PfeAvatar extends PFElement {
123129
);
124130
if (pattern) {
125131
pattern.shift();
132+
console.log("pattern", pattern);
126133
const color = pattern.map(c => parseInt(c, 16));
134+
console.log("color", color);
127135
this._registerColor(color);
128136
} else {
129137
this.log(`[pfe-avatar] invalid color ${colorCode}`);
@@ -136,15 +144,16 @@ class PfeAvatar extends PFElement {
136144

137145
static _registerColor(color) {
138146
PfeAvatar.colors.push({
139-
color1: `rgb(${color.join(",")})`,
140-
color2: `rgb(${this._adjustColor(color).join(",")})`
147+
color1: rgb2hex(...color),
148+
color2: rgb2hex(...this._adjustColor(color))
141149
});
142150
}
143151

144152
static _adjustColor(color) {
145153
const dark = 0.1;
146154
const l_adj = 0.1; // luminance adjustment
147155
const hsl = rgb2hsl(...color);
156+
console.log(`${color} -> ${hsl}`);
148157

149158
// if luminance is too dark already, then lighten the alternate color
150159
// instead of darkening it.

0 commit comments

Comments
 (0)