主要核心
对类似#00000000的色值只改变透明度转成#FF000000
对#000000的色值转成#FFFFFF
- 正则匹配色值 #[0-9a-fA-F]*
- 单选、多选、全选xml替换
- 主要类
public class CopyBoardDarkModeAction extends AnAction {
public CopyBoardDarkModeAction() {
super("转化色值");
}
private static Pattern colorMatcher = Pattern.compile("#[0-9a-fA-F]*");
private String apppendZero(String reds) {
return reds.length() == 1 ? "0".concat(reds) : reds;
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
Editor editor = e.getData(PlatformDataKeys.EDITOR);
if (editor == null)
return;
Document document = editor.getDocument();
SelectionModel selectionModel = editor.getSelectionModel();
convertSelect(e.getProject(), selectionModel, document);
}
private void convertSingle(Project project, SelectionModel selectionModel, Document document, int length) {
int start = selectionModel.getSelectionStart();
int end = selectionModel.getSelectionEnd();
String group = selectionModel.getSelectedText().trim();
String reds, greens, blues;
Runnable runnable = null;
if (length == 6) {
reds = Integer.toHexString(255 - Integer.parseInt(group.substring(0, 2), 16)).toUpperCase();
greens = Integer.toHexString(255 - Integer.parseInt(group.substring(2, 4), 16)).toUpperCase();
blues = Integer.toHexString(255 - Integer.parseInt(group.substring(4, 6), 16)).toUpperCase();
String red = apppendZero(reds);
String green = apppendZero(greens);
String blue = apppendZero(blues);
runnable = () -> document.replaceString(start, end, red + green + blue);
} else if (length == 8) {
//只改变透明度
String alpha = Integer.toHexString(255 - Integer.parseInt(group.substring(0, 2), 16)).toUpperCase();
String color = group.substring(2, 8);
runnable = () -> document.replaceString(start, end, alpha.concat(color));
}
WriteCommandAction.runWriteCommandAction(project, runnable);
}
private void convertSelect(Project project, SelectionModel selectionModel, Document document) {
int length = selectionModel.getSelectedText().length();
if (length == 6 || length == 8) {
convertSingle(project, selectionModel, document, length);
return;
}
Matcher matcher = colorMatcher.matcher(selectionModel.getSelectedText());
while (matcher.find()) {
int start = selectionModel.getSelectionStart() + matcher.start();
int end = selectionModel.getSelectionStart() + matcher.end();
String group = matcher.group();
Runnable runnable = null;
if (group.length() == 7) {
String reds = Integer.toHexString(255 - Integer.parseInt(group.substring(1, 3), 16)).toUpperCase();
String greens = Integer.toHexString(255 - Integer.parseInt(group.substring(3, 5), 16)).toUpperCase();
String blues = Integer.toHexString(255 - Integer.parseInt(group.substring(5, 7), 16)).toUpperCase();
String red = apppendZero(reds);
String green = apppendZero(greens);
String blue = apppendZero(blues);
runnable = () -> document.replaceString(start, end, "#" + red + green + blue);
} else if (group.length() == 9) {
//只改变透明度
String alpha = Integer.toHexString(255 - Integer.parseInt(group.substring(1, 3), 16)).toUpperCase();
String color = group.substring(3, 9);
runnable = () -> document.replaceString(start, end, "#" + alpha.concat(color));
}
WriteCommandAction.runWriteCommandAction(project, runnable);
}
}
}
AS插件开发可参考
AndroidStudio插件开发(进阶篇之Editor)
https://www.jianshu.com/p/59a0826841fe