Skip to content

Commit

Permalink
New Features[colorName <-> Hex] colorName lowercase control by setting.
Browse files Browse the repository at this point in the history
  • Loading branch information
obetame committed May 5, 2018
1 parent 22e306e commit 0e44d30
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,10 @@
# CHANGELOG

- 2.2.0 (2018/5/5)
- Support css color name and hex conversion.
- Support converting the CSS colorname in the entire file to hex.
- Fix text display bug.

- 2.1.0 (2018/4/25)
- add feature (`hex_convert_to_colorname`)

Expand Down
69 changes: 58 additions & 11 deletions ColorConvert.py
Expand Up @@ -25,6 +25,8 @@ class ColorConvertCommand(sublime_plugin.TextCommand):
def __init__(self, view):
# Load settings
self.view = view
global convertMode
global capitalization

settings = sublime.load_settings("ColorConvert.sublime-settings")
capitalization = settings.get("capitalization")
Expand Down Expand Up @@ -74,24 +76,21 @@ def selectModeReplace(self, regions):
"""replace view all color"""
def allReplace(self):
for name in ['rgb', 'rgba', 'hsl', 'hsla', 'hex', 'cmyk', 'hsv']:
firstMatchRegion = self.view.find(util.matchRE.get(name), 0, sublime.IGNORECASE)
currentMatchRegion = self.view.find(util.matchRE.get(name), 0, sublime.IGNORECASE)
allMatchRegin = self.view.find_all(util.matchRE.get(name), sublime.IGNORECASE)

if name == self.innerConvertMode:
continue

for i in range(len(allMatchRegin)):
if firstMatchRegion.empty():
outputs.append(1)
if currentMatchRegion.empty():
continue

output = util.convertColor(self.view.substr(firstMatchRegion), self.innerConvertMode) # core handle function
output = util.convertColor(self.view.substr(currentMatchRegion), self.innerConvertMode) # core handle function
if output != None:
if capitalization:
output = output.upper()
# outputs.append(output)
self.view.replace(self.edit, firstMatchRegion, output)
firstMatchRegion = self.view.find(util.matchRE.get(name), firstMatchRegion.end(), sublime.IGNORECASE)
self.view.replace(self.edit, currentMatchRegion, convertCase(output))

currentMatchRegion = self.view.find(util.matchRE.get(name), currentMatchRegion.end(), sublime.IGNORECASE)

class ColorConvertNameToHexCommand(sublime_plugin.TextCommand):
"""color name convert to hex
Expand Down Expand Up @@ -122,7 +121,7 @@ def convertColorName(self):
for i, output in enumerate(outputs):
for j, region in enumerate(regions):
if i == j and not region.empty():
self.view.replace(self.edit, region, output)
self.view.replace(self.edit, region, convertCase(output))

class ColorConvertHexToNameCommand(sublime_plugin.TextCommand):
"""HEX convert To ColorName"""
Expand Down Expand Up @@ -151,7 +150,55 @@ def covertColorName(self):
for i, output in enumerate(outputs):
for j, region in enumerate(regions):
if i == j and not region.empty():
self.view.replace(self.edit, region, output.lower())
self.view.replace(self.edit, region, convertCase(output, True))

class ColorConvertAllHexToNameCommand(sublime_plugin.TextCommand):
"""all HEX convert To ColorName"""
def __init__(self, view):
self.view = view

# main
def run(self, edit):
self.edit = edit

self.covertColorName()

def covertColorName(self):
"""covert color name"""
currentMatchRegion = self.view.find(util.matchRE.get('hex'), 0, sublime.IGNORECASE)
allMatchRegin = self.view.find_all(util.matchRE.get('hex'), sublime.IGNORECASE)
hexsDict = colorName.getHexColorNameData()

for i in range(len(allMatchRegin)):
if currentMatchRegion.empty():
continue

select = self.view.substr(currentMatchRegion)
if select != None:
hexName = hexsDict.get(select.upper())
if hexName:
print(hexName)
self.view.replace(self.edit, currentMatchRegion, convertCase(hexName, True))

currentMatchRegion = self.view.find(util.matchRE.get('hex'), currentMatchRegion.end(), sublime.IGNORECASE)

def convertCase(value, isColorName = False):
"""Change case according to configuration
Arguments:
value {[str]} -- value data.
isColorName {boolean} -- color name use Hump uppercase
Returns:
[str] -- Case value
"""

if capitalization:
if isColorName:
return colorName.mapColorName.get(value.lower())
return value.upper()

return value.lower()

def loadSettings():
"""Loads settings from the ColorConvert.sublime-settings file"""
Expand Down
14 changes: 9 additions & 5 deletions Context.sublime-menu
Expand Up @@ -12,7 +12,7 @@
"args": {"value": "", "isSelect": true}
},
{
"caption": "Convert Selected Section To",
"caption": "Convert Selected Section ->",
"children":[
{
"caption": "RGB",
Expand Down Expand Up @@ -53,14 +53,14 @@
]
},
{
"caption": "Covert Selected ColorName_HEX",
"caption": "Convert Selected ColorName<->HEX",
"children": [
{
"caption": "ColorName To HEX",
"caption": "ColorName -> HEX",
"command": "color_convert_name_to_hex"
},
{
"caption": "HEX To ColorName",
"caption": "HEX -> ColorName",
"command": "color_convert_hex_to_name"
}
]
Expand All @@ -74,7 +74,7 @@
"args": {"value": "", "isSelect": false}
},
{
"caption": "Convert All To",
"caption": "Convert All ->",
"children":[
{
"caption": "RGB",
Expand Down Expand Up @@ -114,6 +114,10 @@
}
]
},
{
"caption": "Convert All Hex->ColorName",
"command": "color_convert_all_hex_to_name"
},
{
"caption": "-"
},
Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -30,6 +30,7 @@ OR:
- Support `RGB`,`RGBA`,`HEX`,`HSL`,`HSLA`,`HSV`,`CMYK` to convert each other.
- Support [css3 color name](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value) convert to `HEX`
- Support convert the color values in the entire page.
- Support css color name and hex conversion

## Notes

Expand Down

0 comments on commit 0e44d30

Please sign in to comment.