MATLAB 屏幕颜色提取器(无须导入图片即可取色)

在学习MATLAB java混编,有一个不错的实例就是屏幕颜色提取器,毕竟matlab如果不调用java库的话实现这个功能还是比较难的,而且比较慢。
已有的屏幕颜色提取器基本上是按一个按钮之后等待几秒然后取当前鼠标位置屏幕上的颜色,但是出于个人使用方便和保证颜色的精确度,我选择还是那种边移动边取色的提取器,效果大概这个样:

在这里插入图片描述


.

功能及使用方式介绍

从动态图就可以我们可以不断地提取鼠标所到处的颜色,可以通过按键A来使其开始或停止取色,同时会显示颜色相关的三种类型数据:

.

通过按键S可以暂时存储颜色,按键C删除存储的颜色:

.

按键D 将当前颜色复制到剪切板,格式:
在这里插入图片描述
.
最下方为基础颜色库,为了方便分享数据直接写在m文件里了,想要改善可以自己写个mat文件,
基础颜色库:


.

完整界面

.

上下文菜单

对于2021a以后版本,增添了上下文菜单,更改了timer停止机制:

.

如图所示,右键颜色块会显示颜色相关数据,点击相关数据则能将其复制到剪切板。


.

完整代码
function getScreenColor
import java.awt.MouseInfo;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;

%CSC: get screen color
global CSCFig CSCAxes 
global CSCDispLabel255 CSCDispLabel1 CSCDispLabel16
global CSCDispDatal255 CSCDispDatal1  CSCDispDatal16
global CSCFuncDispLabel
global CSCColorStoreLabel CSCBaseColorStoreLabel
global colorStoreBkg colorStore baseColorStoreBkg baseColorStore
global selector

selector.presentColor=[255 255 255];
selector.color255='255 255 255';
selector.color01='1.00 1.00 1.00';
selector.color16='#FFFFFF';
selector.enable=1;
selector.colorList=ones(20,3);
selector.colorNum=0;
selector.listNum=2;
baseColorList=[ 25 202 173;140 199 181;160 238 225;190 231 233;190 237 199;
               214 213 183;209 186 116;230 206 172;236 173 158;244  96 108;
                 0 197 205;102 205 170;121 205 205;187 255 255;255 250 240;
               253 245 230;250 240 230;250 235 215;255 235 205;255 218 185;
                24 116 205;122 103 238;159 121 238; 79 148 205;209  95 238;
               205 150 205;255 174 185;255 130 171;205  96 144;205  50 120;
               230 230 250;240 248 255;255 245 238;240 255 240;245 255 250;
               255 255 240;255 250 205];



%mousepoint=MouseInfo.getPointerInfo().getLocation();
%robot=Robot();
%tempColor=robot.getPixelColor(mousepoint.x, mousepoint.y);
%RGBColor=[tempColor.getRed(),tempColor.getGreen(),tempColor.getBlue()]
%==========================================================================
CSCFig=uifigure('units','pixels',...
    'position',[10 280 300 400],...
    'Numbertitle','off',...
    'menubar','none',...
    'resize','off',...
    'name','getScreenColor 1.0',...
    'color',[1,1,1].*0.97);
CSCAxes=uiaxes('Units','pixels',...
      'parent',CSCFig,...
      'PlotBoxAspectRatio',[1 1 1],...
      'Position',[10 400-10-120 120 120],...
      'Color',[0.99 0.99 0.99],...
      'Box','on', ...
      'XLim',[0 1],'YLim',[0 1],...
      'XTick',[],'YTick',[]);
  
CSCDispLabel255=uilabel(CSCFig,'Text','  0-255','HorizontalAlignment','left',...
    'BackgroundColor',[1 1 1].*0.8,'Position',[130+10,400-10-30-5,140,30],'FontSize',13);
CSCDispLabel1=uilabel(CSCFig,'Text','  0 - 1','HorizontalAlignment','left',...
    'BackgroundColor',[1 1 1].*0.8,'Position',[130+10,400-10-30-40-5,140,30],'FontSize',13);
CSCDispLabel16=uilabel(CSCFig,'Text','  # 16','HorizontalAlignment','left',...
    'BackgroundColor',[1 1 1].*0.8,'Position',[130+10,400-10-30-80-5,140,30],'FontSize',14);

CSCFuncDispLabel=uilabel(CSCFig,'Text','  A:开始/停止取色  S:储存颜色  D:复制到粘贴板','HorizontalAlignment','left',...
    'BackgroundColor',[0.95 0.95 0.95],'Position',[10,240,280,30],'FontSize',12.5,'FontColor',[0.35 0.35 0.35],'FontWeight','bold');

CSCDispDatal255=uieditfield(CSCFig,'Value','255 255 255','HorizontalAlignment','center','Position',[130+10+45,400-10-30-5,95,30]);
CSCDispDatal1=uieditfield(CSCFig,'Value','1.00 1.00 1.00','HorizontalAlignment','center','Position',[130+10+45,400-10-30-40-5,95,30]);
CSCDispDatal16=uieditfield(CSCFig,'Value','#FFFFFF','HorizontalAlignment','center','Position',[130+10+45,400-10-30-80-5,95,30]);
%--------------------------------------------------------------------------
CSCColorStoreLabel=uilabel(CSCFig,'Text',' 自定义颜色(按键C删除):','HorizontalAlignment','left','VerticalAlignment','top',...
    'BackgroundColor',[1 1 1].*0.9,'Position',[15,160,275,80],'FontSize',13,'FontColor',[0.3 0.3 0.35],'FontWeight','bold');
for i=1:2
    for j=1:10
        colorStoreBkg(i,j)=uilabel(CSCFig,'Text','','BackgroundColor',[1 1 1].*0.7,...
            'Position',[18+(j-1)*27-1+1,192-(i-1)*27-1,24,24]);
        colorStore(i,j)=uilabel(CSCFig,'Text','','BackgroundColor',[1 1 1],...
            'Position',[18+(j-1)*27+1,192-(i-1)*27,22,22]);
    end
end
%--------------------------------------------------------------------------
CSCBaseColorStoreLabel=uilabel(CSCFig,'Text',' 基础颜色库:','HorizontalAlignment','left','VerticalAlignment','top',...
    'BackgroundColor',[1 1 1].*0.9,'Position',[15,10,275,135],'FontSize',14,'FontColor',[0.3 0.3 0.35],'FontWeight','bold');
for i=1:4
    for j=1:10
        if (i-1)*10+j<=37
            baseColorStoreBkg(i,j)=uilabel(CSCFig,'Text','','BackgroundColor',[1 1 1].*0.7,...
                'Position',[18+(j-1)*27-1+1,192-(i-1)*27-1-95,24,24]);
            if (i-1)*10+j<=size(baseColorList,1)
                tempStoreColor=baseColorList((i-1)*10+j,:);
            else
                tempStoreColor=[255 255 255];
            end
            baseColorStore(i,j)=uilabel(CSCFig,'Text','','BackgroundColor',tempStoreColor./255,...
                'Position',[18+(j-1)*27+1,192-(i-1)*27-95,22,22]);
    
        end
    end
end
%--------------------------------------------------------------------------
%上下文菜单:
%需要至少2020a版本MATLAB可以实现
%==========================================================================
CSCAxes.Toolbar.Visible='off';
set(CSCFig,'KeyPressFcn',@keyPressFcn) 
fps=10;
CSCtimer=timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @refreshColor);
start(CSCtimer)
%==========================================================================
    function keyPressFcn(~,event)
        switch event.Key
            case 'a'
                if selector.enable==1
                    stop(CSCtimer),selector.enable=0;
                elseif selector.enable==0
                    start(CSCtimer),selector.enable=1;
                end
            case 's'
                if selector.colorNum<20
                    selector.colorList(selector.colorNum+1,:)=selector.presentColor;
                    selector.colorNum=selector.colorNum+1;
                    tempRow=ceil(selector.colorNum/10);
                    tempCol=mod(selector.colorNum,10);tempCol(tempCol==0)=10;
                    set(colorStore(tempRow,tempCol),'BackgroundColor',selector.presentColor./255)
                end
            case 'd'
                clip=Toolkit.getDefaultToolkit().getSystemClipboard();
                tempStr=['【0-255】',selector.color255,' ','【0-1】',selector.color01,' ','【#16】',selector.color16,' '];
                tText=StringSelection(tempStr);
                clip.setContents(tText,[]); 
            case 'c'
                if selector.colorNum>0
                    selector.colorList(selector.colorNum,:)=[1 1 1];
                    selector.colorNum=selector.colorNum;
                    tempRow=ceil(selector.colorNum/10);
                    tempCol=mod(selector.colorNum,10);tempCol(tempCol==0)=10;
                    selector.colorNum=selector.colorNum-1;
                    set(colorStore(tempRow,tempCol),'BackgroundColor',[1 1 1])
                end
                
        end
    end
%==========================================================================
    function refreshColor(~,~)
        mousepoint=MouseInfo.getPointerInfo().getLocation();
        robot=Robot();
        tempColor=robot.getPixelColor(mousepoint.x, mousepoint.y);
        RGBColor=[tempColor.getRed(),tempColor.getGreen(),tempColor.getBlue()];
        if selector.enable==1,selector.presentColor=RGBColor;end
        CSCAxes.Color=(selector.presentColor)./255;
        CSCDispDatal255.Value=[num2str(selector.presentColor(1)),' ',...
                              num2str(selector.presentColor(2)),' ',...
                              num2str(selector.presentColor(3)),' '];
        selector.color255=CSCDispDatal255.Value;
        
        tempColorR=sprintf('%.2f',selector.presentColor(1)./255);
        tempColorG=sprintf('%.2f',selector.presentColor(2)./255);
        tempColorB=sprintf('%.2f',selector.presentColor(3)./255);
        CSCDispDatal1.Value=[tempColorR,' ',tempColorG,' ',tempColorB];
        selector.color01=CSCDispDatal1.Value;
        
        exchange_list={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        tempColor16='#';
        for ii=1:3
            temp_num=selector.presentColor(ii);
            tempColor16(1+ii*2-1)=exchange_list{(temp_num-mod(temp_num,16))/16+1};
            tempColor16(1+ii*2)=exchange_list{mod(temp_num,16)+1};
        end
        CSCDispDatal16.Value=tempColor16;
        selector.color16=CSCDispDatal16.Value;
    end


end
2021a后版本代码
function getScreenColor
import java.awt.MouseInfo;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
clip=Toolkit.getDefaultToolkit().getSystemClipboard();
robot=Robot();

%CSC: get screen color
global CSCFig CSCAxes 
global CSCDispLabel255 CSCDispLabel1 CSCDispLabel16
global CSCDispDatal255 CSCDispDatal1  CSCDispDatal16
global CSCFuncDispLabel
global CSCColorStoreLabel CSCBaseColorStoreLabel
global colorStoreBkg colorStore baseColorStoreBkg baseColorStore
global selector

selector.presentColor=[255 255 255];
selector.color255='255 255 255';
selector.color01='1.00 1.00 1.00';
selector.color16='#FFFFFF';
selector.enable=1;
selector.colorList=ones(20,3);
selector.colorNum=0;
selector.listNum=2;
baseColorList=[ 25 202 173;140 199 181;160 238 225;190 231 233;190 237 199;
               214 213 183;209 186 116;230 206 172;236 173 158;244  96 108;
                 0 197 205;102 205 170;121 205 205;187 255 255;255 250 240;
               253 245 230;250 240 230;250 235 215;255 235 205;255 218 185;
                24 116 205;122 103 238;159 121 238; 79 148 205;209  95 238;
               205 150 205;255 174 185;255 130 171;205  96 144;205  50 120;
               230 230 250;240 248 255;255 245 238;240 255 240;245 255 250;
               255 255 240;255 250 205];



%mousepoint=MouseInfo.getPointerInfo().getLocation();
%robot=Robot();
%tempColor=robot.getPixelColor(mousepoint.x, mousepoint.y);
%RGBColor=[tempColor.getRed(),tempColor.getGreen(),tempColor.getBlue()]
%==========================================================================
CSCFig=uifigure('units','pixels',...
    'position',[10 280 300 400],...
    'Numbertitle','off',...
    'menubar','none',...
    'resize','off',...
    'name','getScreenColor 1.0',...
    'color',[1,1,1].*0.97);
CSCAxes=uiaxes('Units','pixels',...
      'parent',CSCFig,...
      'PlotBoxAspectRatio',[1 1 1],...
      'Position',[10 400-10-120 120 120],...
      'Color',[0.99 0.99 0.99],...
      'Box','on', ...
      'XLim',[0 1],'YLim',[0 1],...
      'XTick',[],'YTick',[]);
  
CSCDispLabel255=uilabel(CSCFig,'Text','  0-255','HorizontalAlignment','left',...
    'BackgroundColor',[1 1 1].*0.8,'Position',[130+10,400-10-30-5,140,30],'FontSize',13);
CSCDispLabel1=uilabel(CSCFig,'Text','  0 - 1','HorizontalAlignment','left',...
    'BackgroundColor',[1 1 1].*0.8,'Position',[130+10,400-10-30-40-5,140,30],'FontSize',13);
CSCDispLabel16=uilabel(CSCFig,'Text','  # 16','HorizontalAlignment','left',...
    'BackgroundColor',[1 1 1].*0.8,'Position',[130+10,400-10-30-80-5,140,30],'FontSize',14);

CSCFuncDispLabel=uilabel(CSCFig,'Text','  A:开始/停止取色  S:储存颜色  D:复制到粘贴板','HorizontalAlignment','left',...
    'BackgroundColor',[0.95 0.95 0.95],'Position',[10,240,280,30],'FontSize',12.5,'FontColor',[0.35 0.35 0.35],'FontWeight','bold');

CSCDispDatal255=uieditfield(CSCFig,'Value','255 255 255','HorizontalAlignment','center','Position',[130+10+45,400-10-30-5,95,30]);
CSCDispDatal1=uieditfield(CSCFig,'Value','1.00 1.00 1.00','HorizontalAlignment','center','Position',[130+10+45,400-10-30-40-5,95,30]);
CSCDispDatal16=uieditfield(CSCFig,'Value','#FFFFFF','HorizontalAlignment','center','Position',[130+10+45,400-10-30-80-5,95,30]);


%==========================================================================
%上下文菜单:
%需要至少2020a版本MATLAB可以实现
ContextMenu=uicontextmenu(CSCFig);
Menu(1)=uimenu(ContextMenu);
Menu(2)=uimenu(ContextMenu);
Menu(3)=uimenu(ContextMenu);
Menu(2).Separator = 'on';
Menu(3).Separator = 'on';
set(ContextMenu,'ContextMenuOpeningFcn',@selectColorLabel)
set(Menu,'MenuSelectedFcn',@selectMenu)
function selectColorLabel(~,~)
    mousepoint=MouseInfo.getPointerInfo().getLocation();
    robot=Robot();
    tempColor=robot.getPixelColor(mousepoint.x, mousepoint.y);
    menuColor=[tempColor.getRed(),tempColor.getGreen(),tempColor.getBlue()];
    Menu(1).Text=[num2str(menuColor(1)),' ',...
                  num2str(menuColor(2)),' ',...
                  num2str(menuColor(3)),' '];
              
    tempColorR=sprintf('%.2f',menuColor(1)./255);
    tempColorG=sprintf('%.2f',menuColor(2)./255);
    tempColorB=sprintf('%.2f',menuColor(3)./255);
    Menu(2).Text=[tempColorR,' ',tempColorG,' ',tempColorB];
    exchange_list={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    tempColor16='#';
        for ii=1:3
            temp_num=menuColor(ii);
            tempColor16(1+ii*2-1)=exchange_list{(temp_num-mod(temp_num,16))/16+1};
            tempColor16(1+ii*2)=exchange_list{mod(temp_num,16)+1};
        end
    Menu(3).Text=tempColor16;
end
    function selectMenu(~,event)
        tText=StringSelection(event.Source.Text);
        clip.setContents(tText,[]);
    end


%==========================================================================
CSCColorStoreLabel=uilabel(CSCFig,'Text',' 自定义颜色(按键C删除):','HorizontalAlignment','left','VerticalAlignment','top',...
    'BackgroundColor',[1 1 1].*0.9,'Position',[15,160,275,80],'FontSize',13,'FontColor',[0.3 0.3 0.35],'FontWeight','bold');
for i=1:2
    for j=1:10
        colorStoreBkg(i,j)=uilabel(CSCFig,'Text','','BackgroundColor',[1 1 1].*0.7,...
            'Position',[18+(j-1)*27-1+1,192-(i-1)*27-1,24,24]);
        colorStore(i,j)=uilabel(CSCFig,'Text','','BackgroundColor',[1 1 1],...
            'Position',[18+(j-1)*27+1,192-(i-1)*27,22,22]);
        set(colorStore(i,j),'ContextMenu',ContextMenu);
    end
end
%--------------------------------------------------------------------------
CSCBaseColorStoreLabel=uilabel(CSCFig,'Text',' 基础颜色库:','HorizontalAlignment','left','VerticalAlignment','top',...
    'BackgroundColor',[1 1 1].*0.9,'Position',[15,10,275,135],'FontSize',14,'FontColor',[0.3 0.3 0.35],'FontWeight','bold');
for i=1:4
    for j=1:10
        if (i-1)*10+j<=37
            baseColorStoreBkg(i,j)=uilabel(CSCFig,'Text','','BackgroundColor',[1 1 1].*0.7,...
                'Position',[18+(j-1)*27-1+1,192-(i-1)*27-1-95,24,24]);
            if (i-1)*10+j<=size(baseColorList,1)
                tempStoreColor=baseColorList((i-1)*10+j,:);
            else
                tempStoreColor=[255 255 255];
            end
            baseColorStore(i,j)=uilabel(CSCFig,'Text','','BackgroundColor',tempStoreColor./255,...
                'Position',[18+(j-1)*27+1,192-(i-1)*27-95,22,22]);
            set(baseColorStore(i,j),'ContextMenu',ContextMenu); 
        end
    end
end


%==========================================================================
CSCAxes.Toolbar.Visible='off';
set(CSCFig,'KeyPressFcn',@keyPressFcn) 
fps=10;
CSCtimer=timer('ExecutionMode', 'FixedRate', 'Period',1/fps, 'TimerFcn', @refreshColor);
start(CSCtimer)

set(CSCFig,'CloseRequestFcn',@closeFig)
function closeFig(~,~)
    stop(CSCtimer);
    delete(CSCFig) 
end


%==========================================================================
    function keyPressFcn(~,event)
        switch event.Key
            case 'a'
                if selector.enable==1
                    stop(CSCtimer),selector.enable=0;
                elseif selector.enable==0
                    start(CSCtimer),selector.enable=1;
                end
            case 's'
                if selector.colorNum<20
                    selector.colorList(selector.colorNum+1,:)=selector.presentColor;
                    selector.colorNum=selector.colorNum+1;
                    tempRow=ceil(selector.colorNum/10);
                    tempCol=mod(selector.colorNum,10);tempCol(tempCol==0)=10;
                    set(colorStore(tempRow,tempCol),'BackgroundColor',selector.presentColor./255)
                end
            case 'd'
                
                tempStr=['【0-255】',selector.color255,' ','【0-1】',selector.color01,' ','【#16】',selector.color16,' '];
                tText=StringSelection(tempStr);
                clip.setContents(tText,[]); 
            case 'c'
                if selector.colorNum>0
                    selector.colorList(selector.colorNum,:)=[1 1 1];
                    selector.colorNum=selector.colorNum;
                    tempRow=ceil(selector.colorNum/10);
                    tempCol=mod(selector.colorNum,10);tempCol(tempCol==0)=10;
                    selector.colorNum=selector.colorNum-1;
                    set(colorStore(tempRow,tempCol),'BackgroundColor',[1 1 1])
                end
                
        end
    end
%==========================================================================
    function refreshColor(~,~)
        mousepoint=MouseInfo.getPointerInfo().getLocation();
        tempColor=robot.getPixelColor(mousepoint.x, mousepoint.y);
        RGBColor=[tempColor.getRed(),tempColor.getGreen(),tempColor.getBlue()];
        if selector.enable==1,selector.presentColor=RGBColor;end
        CSCAxes.Color=(selector.presentColor)./255;
        CSCDispDatal255.Value=[num2str(selector.presentColor(1)),' ',...
                              num2str(selector.presentColor(2)),' ',...
                              num2str(selector.presentColor(3)),' '];
        selector.color255=CSCDispDatal255.Value;
        
        tempColorR=sprintf('%.2f',selector.presentColor(1)./255);
        tempColorG=sprintf('%.2f',selector.presentColor(2)./255);
        tempColorB=sprintf('%.2f',selector.presentColor(3)./255);
        CSCDispDatal1.Value=[tempColorR,' ',tempColorG,' ',tempColorB];
        selector.color01=CSCDispDatal1.Value;
        
        exchange_list={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        tempColor16='#';
        for ii=1:3
            temp_num=selector.presentColor(ii);
            tempColor16(1+ii*2-1)=exchange_list{(temp_num-mod(temp_num,16))/16+1};
            tempColor16(1+ii*2)=exchange_list{mod(temp_num,16)+1};
        end
        CSCDispDatal16.Value=tempColor16;
        selector.color16=CSCDispDatal16.Value;
    end


end
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

slandarer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值