Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

Commit

Permalink
Parse hex colors with alpha channels
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Jun 20, 2018
1 parent 8d70c2c commit 8869aee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
5 changes: 5 additions & 0 deletions doc-src/SASS_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
* Add support for importing an `_index.scss` or `_index.sass` file when
importing a directory.

### Backwards Incompatibilities -- Must Read!

* Tokens such as `#abcd` that are ambiguous between ID strings and hex colors
with an alpha channel are now parsed as colors.

## 3.5.6 (22 March 2018)

* Allow `!` in custom property values.
Expand Down
20 changes: 8 additions & 12 deletions lib/sass/script/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,24 +368,20 @@ def id
# IDs in properties are used in the Basic User Interface Module
# (http://www.w3.org/TR/css3-ui/).
return unless scan(REGULAR_EXPRESSIONS[:id])
if @scanner[0] =~ /^\#[0-9a-fA-F]+$/
if @scanner[0].length == 4 || @scanner[0].length == 7
return [:color, Script::Value::Color.from_hex(@scanner[0])]
elsif @scanner[0].length == 5 || @scanner[0].length == 9
filename = @options[:filename]
Sass::Util.sass_warn <<MESSAGE
DEPRECATION WARNING on line #{line}, column #{offset}#{" of #{filename}" if filename}:
The value "#{@scanner[0]}" is currently parsed as a string, but it will be parsed as a color in
future versions of Sass. Use "unquote('#{@scanner[0]}')" to continue parsing it as a string.
MESSAGE
end
if @scanner[0] =~ /^\#[0-9a-fA-F]+$/ &&
(@scanner[0].length == 4 || @scanner[0].length == 5 ||
@scanner[0].length == 7 || @scanner[0].length == 9)
return [:color, Script::Value::Color.from_hex(@scanner[0])]
end
[:ident, @scanner[0]]
end

def color
return unless @scanner.match?(REGULAR_EXPRESSIONS[:color])
return unless @scanner[0].length == 4 || @scanner[0].length == 7
unless @scanner[0].length == 4 || @scanner[0].length == 5 ||
@scanner[0].length == 7 || @scanner[0].length == 9
return
end
script_color = Script::Value::Color.from_hex(scan(REGULAR_EXPRESSIONS[:color]))
[:color, script_color]
end
Expand Down
5 changes: 3 additions & 2 deletions lib/sass/script/value/color.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,14 @@ def initialize(attrs, representation = nil, allow_both_rgb_and_hsl = false)
#
# @return [Color]
def self.from_hex(hex_string, alpha = nil)
unless hex_string =~ /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i ||
hex_string =~ /^#?([0-9a-f])([0-9a-f])([0-9a-f])$/i
unless hex_string =~ /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i ||
hex_string =~ /^#?([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])?$/i
raise ArgumentError.new("#{hex_string.inspect} is not a valid hex color.")
end
red = $1.ljust(2, $1).to_i(16)
green = $2.ljust(2, $2).to_i(16)
blue = $3.ljust(2, $3).to_i(16)
alpha = $4.ljust(2, $4).to_i(16).to_f/0xff if $4

hex_string = "##{hex_string}" unless hex_string[0] == ?#
attrs = {:red => red, :green => green, :blue => blue, :representation => hex_string}
Expand Down

0 comments on commit 8869aee

Please sign in to comment.