As深色模式色值转换插件

主要核心
对类似#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值