From 6c38bc8bb4d7e86359d05c8ae5e82f229504cc7f Mon Sep 17 00:00:00 2001 From: yutent Date: Fri, 8 Mar 2024 18:01:21 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A2=9C=E8=89=B2=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/attr.js | 37 +++++++------------------------------ src/filter.js | 2 -- src/lib/color.js | 39 +++++---------------------------------- src/lib/constants.js | 2 +- 4 files changed, 13 insertions(+), 67 deletions(-) diff --git a/src/attr.js b/src/attr.js index 64f2273..64910a6 100644 --- a/src/attr.js +++ b/src/attr.js @@ -1,21 +1,13 @@ -// Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +/** + * {} + * @author yutent + * @date 2024/03/08 17:59:50 + */ + import eve from './eve.js' import { Snap, SnapElement, Paper, Fragment, getSomeDefs } from './svg.js' import { $, is, url } from './utils.js' import { doc, win, SEPARATOR } from './lib/constants.js' -import { parseColor } from './lib/color.js' let has = 'hasOwnProperty', reURLValue = /^url\((['"]?)([^)]+)\1\)$/, @@ -121,22 +113,7 @@ function fillStroke(name) { fill = value.attr(name) } } else { - fill = parseColor(value) - if (fill.error) { - let grad = new Snap(getSomeDefs(this).ownerSVGElement).gradient(value) - if (grad) { - if (!grad.node.id) { - $(grad.node, { - id: grad.id - }) - } - fill = url(grad.node.id) - } else { - fill = value - } - } else { - fill = Str(fill) - } + fill = String(value) } let attrs = {} attrs[name] = fill diff --git a/src/filter.js b/src/filter.js index 4c66b97..79fbb89 100644 --- a/src/filter.js +++ b/src/filter.js @@ -7,7 +7,6 @@ import eve from './eve.js' import { select, SnapElement } from './svg.js' import { $, url } from './utils.js' -import { parseColor } from './lib/color.js' let rgurl = /^\s*url\((.+)\)/, Str = String @@ -84,7 +83,6 @@ export const filter = { }); */ shadow(dx = 0, dy = 2, blur = 4, color = '#000', opacity = 1) { - color = parseColor(color) return `` }, diff --git a/src/lib/color.js b/src/lib/color.js index 2a1c7b4..2cd70e5 100644 --- a/src/lib/color.js +++ b/src/lib/color.js @@ -5,6 +5,7 @@ */ import { cacher, is } from '../utils.js' +import { COMMA_SPACES } from './constants.js' const hsrg = { hs: 1, rg: 1 } const colourRegExp = @@ -73,37 +74,7 @@ function packageRGB(r, g, b, o) { } // Colour -/*\ - ** - * Parses color string as RGB object - - color (string) color string in one of the following formats: - #
    - #
  • Color name (red, green, cornflowerblue, etc)
  • - #
  • #••• — shortened HTML color: (#000, #fc0, etc.)
  • - #
  • #•••••• — full length HTML color: (#000000, #bd2300)
  • - #
  • rgb(•••, •••, •••) — red, green and blue channels values: (rgb(200, 100, 0))
  • - #
  • rgba(•••, •••, •••, •••) — also with opacity
  • - #
  • rgb(•••%, •••%, •••%) — same as above, but in %: (rgb(100%, 175%, 0%))
  • - #
  • rgba(•••%, •••%, •••%, •••%) — also with opacity
  • - #
  • hsb(•••, •••, •••) — hue, saturation and brightness values: (hsb(0.5, 0.25, 1))
  • - #
  • hsba(•••, •••, •••, •••) — also with opacity
  • - #
  • hsb(•••%, •••%, •••%) — same as above, but in %
  • - #
  • hsba(•••%, •••%, •••%, •••%) — also with opacity
  • - #
  • hsl(•••, •••, •••) — hue, saturation and luminosity values: (hsb(0.5, 0.25, 0.5))
  • - #
  • hsla(•••, •••, •••, •••) — also with opacity
  • - #
  • hsl(•••%, •••%, •••%) — same as above, but in %
  • - #
  • hsla(•••%, •••%, •••%, •••%) — also with opacity
  • - #
- * Note that `%` can be used any time: `rgb(20%, 255, 50%)`. - = (object) RGB object in the following format: - o { - o r (number) red, - o g (number) green, - o b (number) blue, - o hex (string) color in HTML/CSS format: #••••••, - o error (boolean) true if string can't be parsed - o } -\*/ + export const getRGB = cacher(function (colour) { if (!colour || !!((colour = String(colour)).indexOf('-') + 1)) { return { @@ -153,7 +124,7 @@ export const getRGB = cacher(function (colour) { red = parseInt((t = rgb[3].charAt(1)) + t, 16) } if (rgb[4]) { - values = rgb[4].split(commaSpaces) + values = rgb[4].split(COMMA_SPACES) red = +values[0] values[0].slice(-1) == '%' && (red *= 2.55) green = +values[1] @@ -164,7 +135,7 @@ export const getRGB = cacher(function (colour) { values[3] && values[3].slice(-1) == '%' && (opacity /= 100) } if (rgb[5]) { - values = rgb[5].split(commaSpaces) + values = rgb[5].split(COMMA_SPACES) red = +values[0] values[0].slice(-1) == '%' && (red /= 100) green = +values[1] @@ -178,7 +149,7 @@ export const getRGB = cacher(function (colour) { return hsb2rgb(red, green, blue, opacity) } if (rgb[6]) { - values = rgb[6].split(commaSpaces) + values = rgb[6].split(COMMA_SPACES) red = +values[0] values[0].slice(-1) == '%' && (red /= 100) green = +values[1] diff --git a/src/lib/constants.js b/src/lib/constants.js index 304a73d..afa03c6 100644 --- a/src/lib/constants.js +++ b/src/lib/constants.js @@ -77,7 +77,7 @@ export const ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i export const SEPARATOR = /[,\s]+/ export const whitespace = /[\s]/g -export const commaSpaces = /[\s]*,[\s]*/ +export const COMMA_SPACES = /[\s]*,[\s]*/ export const bezierrg = /^(?:cubic-)?bezier\(([^,]+),([^,]+),([^,]+),([^\)]+)\)/ export const pathCommand = /([a-z])[\s,]*((-?\d*\.?\d*(?:e[\-+]?\d+)?[\s]*,?[\s]*)+)/gi