在学习MATLAB java混编,有一个不错的实例就是屏幕颜色提取器,毕竟matlab如果不调用java库的话实现这个功能还是比较难的,而且比较慢。
已有的屏幕颜色提取器基本上是按一个按钮之后等待几秒然后取当前鼠标位置屏幕上的颜色,但是出于个人使用方便和保证颜色的精确度,我选择还是那种边移动边取色的提取器,效果大概这个样:
.
从动态图就可以我们可以不断地提取鼠标所到处的颜色,可以通过按键A来使其开始或停止取色,同时会显示颜色相关的三种类型数据:
.通过按键S可以暂时存储颜色,按键C删除存储的颜色:
.按键D 将当前颜色复制到剪切板,格式:
.
最下方为基础颜色库,为了方便分享数据直接写在m文件里了,想要改善可以自己写个mat文件,
基础颜色库:
.
.
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