From a57442284e4d7ea5c4a9314ada9d5fff3997d556 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Thu, 13 Jan 2022 13:56:36 +0800 Subject: [PATCH] feat: post tags support color settings (halo-dev/console#395) * feat: post tags support color settings * feat: add tag color picker * feat: support choose tag color Co-authored-by: guqing <1484563614@qq.com> --- src/components/Post/PostTag.vue | 28 ++++++++++++++++++++ src/components/index.js | 4 ++- src/utils/colorUtil.js | 47 +++++++++++++++++++++++++++++++++ src/views/post/PostList.vue | 4 +-- src/views/post/TagList.vue | 39 ++++++++++++++++++--------- 5 files changed, 107 insertions(+), 15 deletions(-) create mode 100644 src/components/Post/PostTag.vue create mode 100644 src/utils/colorUtil.js diff --git a/src/components/Post/PostTag.vue b/src/components/Post/PostTag.vue new file mode 100644 index 000000000..c6254626e --- /dev/null +++ b/src/components/Post/PostTag.vue @@ -0,0 +1,28 @@ + + diff --git a/src/components/index.js b/src/components/index.js index 7c4c065d1..8c171da05 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -6,6 +6,7 @@ import FilePondUpload from '@/components/Upload/FilePondUpload' import AttachmentSelectDrawer from './Attachment/AttachmentSelectDrawer' import AttachmentUploadModal from './Attachment/AttachmentUploadModal' import ReactiveButton from './Button/ReactiveButton' +import PostTag from './Post/PostTag' const _components = { Ellipsis, @@ -13,7 +14,8 @@ const _components = { FilePondUpload, AttachmentSelectDrawer, AttachmentUploadModal, - ReactiveButton + ReactiveButton, + PostTag } const components = {} diff --git a/src/utils/colorUtil.js b/src/utils/colorUtil.js new file mode 100644 index 000000000..260805118 --- /dev/null +++ b/src/utils/colorUtil.js @@ -0,0 +1,47 @@ +const isLight = color => { + if (!isHex(color)) { + return false + } + + // Check the format of the color, HEX or RGB? + let r, g, b, hsp + if (color.match(/^rgb/)) { + // If HEX --> store the red, green, blue values in separate variables + color = color.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+(?:\.\d+)?))?\)$/) + + r = color[1] + g = color[2] + b = color[3] + } else { + // If RGB --> Convert it to HEX: http://gist.github.com/983661 + color = +('0x' + color.slice(1).replace(color.length < 5 && /./g, '$&$&')) + + r = color >> 16 + g = (color >> 8) & 255 + b = color & 255 + } + + // HSP (Highly Sensitive Poo) equation from http://alienryderflex.com/hsp.html + hsp = Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b)) + + // Using the HSP value, determine whether the color is light or dark + return hsp > 127.5 +} + +const randomHex = () => { + return '#' + Math.floor(Math.random() * 16777215).toString(16) +} +const hexRegExp = /(^#[0-9A-F])/i + +const isHex = color => { + return hexRegExp.test(color) +} + +const isRgb = color => { + return /^rgb/.test(color) +} +const isHsl = color => { + return /^hsl/.test(color) +} + +export { isLight, randomHex, isHex, hexRegExp, isRgb, isHsl } diff --git a/src/views/post/PostList.vue b/src/views/post/PostList.vue index 7aadf3f08..72fb6cb09 100644 --- a/src/views/post/PostList.vue +++ b/src/views/post/PostList.vue @@ -201,7 +201,7 @@ {{ tag.name }} @@ -268,7 +268,7 @@ - + {{ tag.name }} diff --git a/src/views/post/TagList.vue b/src/views/post/TagList.vue index 325f30de1..a9f075b04 100644 --- a/src/views/post/TagList.vue +++ b/src/views/post/TagList.vue @@ -10,6 +10,13 @@ + + + + + @@ -63,9 +70,7 @@ - {{ tag.name }} - + @@ -83,9 +88,15 @@