Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

SASS "error: $color: is not a color" on unquoted string

I am passing in variables to sass using gulp-sass-variables, which will always inject a string.

Unquote doesn't seem to have any effect on my string.

$primary: "#ffffff"; // <- Passed in via gulp-sass-variables

$anchor: scale-color(unquote($primary), $lightness: -14%); // Error: $color: #ffffff is not a color.

Am I using unquote in the wrong way?

question from:https://stackoverflow.com/questions/65829863/sass-error-color-is-not-a-color-on-unquoted-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to use unquote, just remove double quotes from color and it should work

$primary: #ffffff; 
$anchor: scale-color($primary, $lightness: -14%); 

Working example: https://www.sassmeister.com/gist/5db400dc2bd9bb62980a8411f158343e

Edited

Can you try the code below and check that if it works:

// Thanks Hugo for convert string to number function
// https://hugogiraudel.com
// Thanks @roshanakjamali

// remove space from string
@function str-replace($string, $search, $replace) {
  $index: str-index($string, $search);

  @if $index {
    @return str-slice($string, 1, $index - 1) + $replace +
      str-replace(
        str-slice($string, $index + str-length($search)),
        $search,
        $replace
      );
  }

  @return $string;
}

// convert string to list
@function str-split($string, $separator) {
  $split-arr: ();
  $index: str-index($string, $separator);
  @while $index != null {
    $item: str-slice($string, 1, $index - 1);
    $split-arr: append($split-arr, unquote($item));
    $string: str-slice($string, $index + 1);
    $index: str-index($string, $separator);
  }
  $split-arr: append($split-arr, unquote($string));
  @return $split-arr;
}

// convert string to number
@function number($string) {
  // Matrices
  $strings: "0" "1" "2" "3" "4" "5" "6" "7" "8" "9";
  $numbers: 0 1 2 3 4 5 6 7 8 9;

  // Result
  $result: 0;

  // Looping through all characters
  @for $i from 1 through str-length($string) {
    $character: str-slice($string, $i, $i);
    $index: index($strings, $character);

    @if not $index {
      @return false;
    }

    $number: nth($numbers, $index);
    $result: $result * 10 + $number;
  }

  @return $result;
}

@function convertStringToColor($colors) {
  $string: str-replace($colors, " ", "");
  $list: str-split($string, ",");
  $red: nth($list, 1);
  $green: nth($list, 2);
  $blue: nth($list, 3);

  $rgb: rgb(number($red), number($green), number($blue));

  @return $rgb;
}

$primary: "255,255,255" !default; // use rgb color without rgb. rgb(255,255,255) => 255,255,255
$anchor: scale-color(convertStringToColor($primary), $lightness: -14%) !default;

Working example: https://codepen.io/sunilmca09/pen/eYdojPV


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...