1
0
mirror of https://github.com/d3/d3.git synced 2025-12-08 19:46:24 +00:00
d3/docs/components/ColorSpan.vue
Philippe Rivière 5e1ef93fb9
add color (#3669)
* add color

* border-bottom: solid 2px color

* ColorSpan component

* more ColorSpan

* fix ColorSpan

---------

Co-authored-by: Mike Bostock <mbostock@gmail.com>
2023-06-15 14:02:33 -04:00

31 lines
748 B
Vue

<script setup>
import * as d3 from "d3";
</script>
<script>
export default {
props: {
color: true,
text: {type: String},
format: {type: String, optional: true}
}
};
function formatColor(color, format) {
switch (format) {
case "rgb": color = d3.rgb(color).formatRgb(); break;
case "hex": color = d3.rgb(color).formatHex(); break;
case "hex8": color = d3.rgb(color).formatHex8(); break;
case "hsl": color = d3.hsl(color).formatHsl(); break;
default: color = String(color); break;
}
return color.replace(/(\d+\.\d+)/g, (d) => +(+d).toFixed(1));
}
</script>
<template>
<em :style="{borderBottom: `solid 2px ${String(color)}`}">{{text === undefined ? formatColor(color, format) : text}}</em>
</template>