I'm trying to simulate a mastercard contactless transaction using a Raspberry Pi with PN532 Chip and a smartwatch. The response from the SFI2 R2 contains the following CDOL1 data:
9F02069F03069F1A0295055F2A029A039C019F37049F35019F45029F4C089F34039F1D089F15029F4E14
which translates to:
TAG LENGTH
9F02 06
9F03 06
9F1A 02
95 05
5F2A 02
9A 03
9C 01
9F37 04
9F35 01
9F45 02
9F4C 08
9F34 03
9F1D 08
9F15 02
9F4E 14
I created the Get Application Cryptogram command following these specifications:
byte_t get_app_crypto[] = {
0x80, 0xAE, // CLA INS
0x80, 0x00, // P1 P2
0x43, // length
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, // amount
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // other amount
0x06, 0x42, // terminal country
0x00, 0x00, 0x00, 0x00, 0x00, // tvr terminal
0x09, 0x46, // currency code
0x20, 0x08, 0x23, // transaction date
0x00, // transaction type
0x11, 0x22, 0x33, 0x44, // UN
0x22, // terminal type
0x00, 0x00,// data auth code
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // icc dynamic
0x00, 0x00, 0x00, // cvm results
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 8
0x54, 0x11, // 2 merchant category
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 14 merchant name or location
0x00, // LE
};
but the response from the watch is always "6700" (wrong length).
Any help would be appreciated.
AFAIR, your question looks like a problem that already had a resolution on SO. Generally, it's good to search.
Back to your issue, you interpreted 9F4E length as 14 decimal, but it simply is 0x14. You are six bytes short.
I want to test the linux usb HID gadget "g_hid" modules. But while inserting it is giving error as "no such device". After referring the Linux Documentation
http://elixir.free-electrons.com/linux/v4.13.11/source/Documentation/usb/gadget_hid.txt, I got to know the following code we need to add in the platform code
#include <linux/platform_device.h>
#include <linux/usb/g_hid.h>
/* hid descriptor for a keyboard */
static struct hidg_func_descriptor my_hid_data = {
.subclass = 0, /* No subclass */
.protocol = 1, /* Keyboard */
.report_length = 8,
.report_desc_length = 63,
.report_desc = {
0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
0x09, 0x06, /* USAGE (Keyboard) */
0xa1, 0x01, /* COLLECTION (Application) */
0x05, 0x07, /* USAGE_PAGE (Keyboard) */
0x19, 0xe0, /* USAGE_MINIMUM (Keyboard LeftControl) */
0x29, 0xe7, /* USAGE_MAXIMUM (Keyboard Right GUI) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x95, 0x08, /* REPORT_COUNT (8) */
0x81, 0x02, /* INPUT (Data,Var,Abs) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x81, 0x03, /* INPUT (Cnst,Var,Abs) */
0x95, 0x05, /* REPORT_COUNT (5) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x05, 0x08, /* USAGE_PAGE (LEDs) */
0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */
0x29, 0x05, /* USAGE_MAXIMUM (Kana) */
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
0x95, 0x01, /* REPORT_COUNT (1) */
0x75, 0x03, /* REPORT_SIZE (3) */
0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */
0x95, 0x06, /* REPORT_COUNT (6) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x65, /* LOGICAL_MAXIMUM (101) */
0x05, 0x07, /* USAGE_PAGE (Keyboard) */
0x19, 0x00, /* USAGE_MINIMUM (Reserved) */
0x29, 0x65, /* USAGE_MAXIMUM (Keyboard Application) */
0x81, 0x00, /* INPUT (Data,Ary,Abs) */
0xc0 /* END_COLLECTION */
}
};
static struct platform_device my_hid = {
.name = "hidg",
.id = 0,
.num_resources = 0,
.resource = 0,
.dev.platform_data = &my_hid_data,
};
Where shall I add the above code in Linux source code? I am using TI-AM57xx board.
You are not supposed to modify any kernel source!
These HID descriptors are what goes into your USB device firmware.
EDIT: reformulating this question as I've managed to get the basics to work, but still experience problems.
I'm trying to emulate a USB device (bar code scanner) for testing purposes using usb-vhci, and I'm having some problems.
To give some context: the device is a CDC abstract modem, and the client - a java program - communicates with it over the serial line using AT commands.
Basically, I've got my device up and running, it registers itself correctly and I'm able to receive commands from and respond to the client.
The main problem appears to be that as soon as the device starts up or receives a bulk transfer from the host it triggers an ongoing stream of bulk and interrupt IN transfers (massive amounts, my usbmon log grows to 100 MB in a few seconds).
First at startup, where it keeps spewing out (mainly) bulk IN transfers until I receive the SET_CONTROL_LINE_STATE request and then they stop. Then, when the client sends the commands (AT command via the serial device) it starts again.
I'm guessing this is because I'm not responding correctly to some transfer, but I can't figure out what it is.
I've been comparing the usbmon output of my device with that of the real device, but so far I haven't been able to detect any difference that would explain why my emulated device behaves like this and the real one doesn't.
I basically started out with the example code found in libusb_vhci/examples/virtual_device2.c and adapted it to mimic the actual device. First off the device descriptors:
const uint8_t dev_desc[] = {
/* Device Descriptor */
0x12, //bLength 18
0x01, //bDescriptorType 1
0x00, 0x02, //bcdUSB 2.00
0x02, //bDeviceClass 2 Communications
0x00, //bDeviceSubClass 0
0x00, //bDeviceProtocol 0
0x40, //bMaxPacketSize0 64
0x5a, 0x06, //idVendor 065a
0x02, 0xa0, //idProduct a002
0x00, 0x01, //bcdDevice 1.00
0x00, //iManufacturer 0
0x01, //iProduct 1
0x00, //iSerial 0
0x01 //bNumConfigurations 1
};
const uint8_t conf_desc[] = {
/* Configuration Descriptor */
0x09, //bLength 9
0x02, //bDescriptorType 2
0x43, 0x00, //wTotalLength 67 ??
0x02, //bNumInterfaces 2
0x01, //bConfigurationValue 1
0x00, //iConfiguration 0
0x80, //bmAttributes (Bus Powered) 0x80
250, //MaxPower 500mA
/* Interface Descriptor 0 */
0x09, //bLength 9
0x04, //bDescriptorType 4
0x00, //bInterfaceNumber 0
0x00, //bAlternateSetting 0
0x01, //bNumEndpoints 1
0x02, //bInterfaceClass 2 Communications
0x02, //bInterfaceSubClass 2 Abstract (modem)
0x00, //bInterfaceProtocol 0 None
0x00, //iInterface 0
/* CDC Header */
0x05, //bLength 7
0x24, //bDescriptorType 5
0x00, //bEndpointAddress 0x01 EP 1 OUT
0x10, //bcdCDC 1.10
0x01, //"
/* CDC Call Management */
0x05, //bLength 3
0x24, //CDC_CS_INTERFACE
0x01, //CDC_CALL_MANAGEMENT
0x01, //bmCapabilities 0x01
0x00, //bDataInterface 0
/* CDC ACM */
0x04, //bLength 2
0x24, //CDC_CS_INTERFACE
0x02, //CDC_ABSTRACT_CONTROL_MANAGEMENT
0x02, //bmCapabilities 0x02
/* CDC Union */
0x05, //bLength 3
0x24, //CDC_CS_INTERFACE
0x06, //CDC_UNION
0x00, //bMasterInterface 0
0x01, //bSlaveInterface 1
/* Endpoint Descriptor */
0x07, //bLength 7
0x05, //bDescriptorType 5
0x83, //bEndpointAddress 0x83 EP 3 IN
0x03, //bmAttributes 3
0x40, 0x00, //wMaxPacketSize 0x0040 1x 64 bytes
0x0a, //bInterval 10
/* Interface Descriptor 1 */
0x09, //bLength 9
0x04, //bDescriptorType 4
0x01, //bInterfaceNumber 1
0x00, //bAlternateSetting 0
0x02, //bNumEndpoints 2
0x0a, //bInterfaceClass 10 CDC Data
0x00, //bInterfaceSubClass 0
0x00, //bInterfaceProtocol 0
0x00, //iInterface 0
/* Endpoint Descriptor */
0x07, //bLength 7
0x05, //bDescriptorType 5
0x01, //bEndpointAddress 0x01 EP 1 OUT
0x02, //bmAttributes 2
0x40, 0x00, //wMaxPacketSize 0x0040 1x 64 bytes
0x00, //bInterval 0
/* Endpoint Descriptor */
0x07, //bLength 7
0x05, //bDescriptorType 5
0x82, //bEndpointAddress 0x82 EP 2 IN
0x02, //bmAttributes 2
0x40,0x00, //wMaxPacketSize 0x0040 1x 64 bytes
0x00 //bInterval 0
};
const uint8_t str0_desc[] = {
0x04, //bLength 4
0x03, //bDescriptorType 3
0x09, 0x04 //bLanguage 0409 US
};
const uint8_t *str1_desc =
(uint8_t *)"\x36\x03O\0p\0t\0i\0c\0o\0n\0 \0U\0S\0B\00\0B\0a\0r\0c\0o\0d\0e\0 \0R\0e\0a\0d\0e\0r";
The main function is the same as in the example, but the process_urb() function is what has mainly been changed. The control section is largely intact, but I've added handling for some additional setup packets:
uint8_t rt = urb->bmRequestType;
uint8_t r = urb->bRequest;
if(rt == 0x00 && r == URB_RQ_SET_CONFIGURATION)
{
devlog("URB_RQ_SET_CONFIGURATION\n");
urb->status = USB_VHCI_STATUS_SUCCESS;
}
else if(rt == 0x00 && r == URB_RQ_SET_INTERFACE)
{
devlog("URB_RQ_SET_INTERFACE\n");
urb->status = USB_VHCI_STATUS_SUCCESS;
}
else if (rt == 0x21 && r == 0x20)
{
devlog("URB_CDC_SET_LINE_CODING\n");
urb->status = USB_VHCI_STATUS_SUCCESS;
}
else if (rt == 0x21 && r == 0x22)
{
devlog("URB_CDC_SET_CONTROL_LINE_STATE\n");
urb->status = USB_VHCI_STATUS_SUCCESS;
}
else if(rt == 0x80 && r == URB_RQ_GET_DESCRIPTOR)
{
int l = urb->wLength;
uint8_t *buffer = urb->buffer;
devlog("GET_DESCRIPTOR ");
switch(urb->wValue >> 8)
{
case 0:
puts("WTF_DESCRIPTOR");
urb->status = USB_VHCI_STATUS_SUCCESS;
break;
case 1:
puts("DEV_DESC");
if(dev_desc[0] < l) l = dev_desc[0];
memcpy(buffer, dev_desc, l);
urb->buffer_actual = l;
urb->status = USB_VHCI_STATUS_SUCCESS;
break;
case 2:
puts("CONF_DESC");
if(conf_desc[2] < l) l = conf_desc[2];
memcpy(buffer, conf_desc, l);
urb->buffer_actual = l;
urb->status = USB_VHCI_STATUS_SUCCESS;
break;
case 3:
devlog(" Reading string %d\n", urb->wValue & 0xff);
switch(urb->wValue & 0xff)
{
case 0:
if(str0_desc[0] < l) l = str0_desc[0];
memcpy(buffer, str0_desc, l);
urb->buffer_actual = l;
urb->status = USB_VHCI_STATUS_SUCCESS;
break;
case 1:
if(str1_desc[0] < l) l = str1_desc[0];
memcpy(buffer, str1_desc, l);
urb->buffer_actual = l;
urb->status = USB_VHCI_STATUS_SUCCESS;
break;
default:
devlog(" Trying to read unknown string: %d\n",urb->wValue & 0xff);
urb->status = USB_VHCI_STATUS_STALL;
break;
}
break;
default:
devlog(" UNKNOWN: wValue=%d (%d)\n",urb->wValue, urb->wValue >> 8);
urb->status = USB_VHCI_STATUS_STALL;
break;
}
}
else
{
devlog("OTHER bmRequestType %x bRequest %x\n", rt, r);
urb->status = USB_VHCI_STATUS_STALL;
}
The main issue is in handling the non-control transfers though. Here's my current implementation:
/* handle non-control sequences */
if(!usb_vhci_is_control(urb->type)) {
/* if we have a BULK OUT transfer */
if (usb_vhci_is_bulk(urb->type) && usb_vhci_is_out(urb->epadr)) {
/* we have a bulk out transfer, i.e. a command from client */
int cmd = get_at_command(urb->buffer, urb->buffer_actual);
if (cmd == COMMAND_Z1) {
/* we have request for version, need to wait for the BULK IN transfer */
last_command = cmd;
}
urb->status = USB_VHCI_STATUS_SUCCESS;
return;
}
/* if we have a BULK IN transfer */
if (usb_vhci_is_bulk(urb->type) && usb_vhci_is_in(urb->epadr)) {
/* we have a BULK IN transfer, use it to respond to any buffered commands */
if (last_command) {
/* send version */
memcpy(urb->buffer, VERSION_STR, strlen(VERSION_STR));
urb->buffer_actual = strlen(VERSION_STR);
last_command = 0;
urb->status = USB_VHCI_STATUS_SUCCESS;
return;
}
}
urb->status = USB_VHCI_STATUS_SUCCESS;
return;
}
Here's a snippet of the usbmon log I get as my device is starting up:
ffff880510727900 266671312 S Bi:5:002:2 -115 128 <
ffff880510727f00 266671315 C Bi:5:002:2 0 0
ffff880510727f00 266671316 S Bi:5:002:2 -115 128 <
ffff880510727cc0 266671319 C Ii:5:002:3 0:8 0
ffff880510727cc0 266671321 S Ii:5:002:3 -115:8 64 <
ffff880514d80900 266671323 S Co:5:002:0 s 21 22 0000 0000 0000 0
ffff880510727780 266671324 C Bi:5:002:2 0 0
ffff880510727780 266671325 S Bi:5:002:2 -115 128 <
ffff8805101096c0 266671329 C Bi:5:002:2 0 0
ffff8805101096c0 266671333 S Bi:5:002:2 -115 128 <
ffff8805107273c0 266671339 C Bi:5:002:2 0 0
ffff8805107273c0 266671344 S Bi:5:002:2 -115 128 <
ffff880510109b40 266671348 C Bi:5:002:2 0 0
ffff880510109b40 266671350 S Bi:5:002:2 -115 128 <
ffff880510109000 266671354 C Bi:5:002:2 0 0
ffff880510109000 266671357 S Bi:5:002:2 -115 128 <
ffff880510727d80 266671360 C Bi:5:002:2 0 0
ffff880510727d80 266671361 S Bi:5:002:2 -115 128 <
ffff880510109a80 266671363 C Bi:5:002:2 0 0
ffff880510109c00 266671370 C Bi:5:002:2 0 0
...
So, this is basically where I'm stuck. I've got a nearly functioning device, but the massive amounts of transfers basically chokes my system rendering it useless. Any help or info would be greatly appreciated!
It seems I have been able to resolve most of my issues now, and the problem was indeed me not responding correctly to events.
After doing some more detailed analysis of the usbmon output for the real device I noticed that it was responding to the superfluous interrupt transfers with -ENOENT, whereas I was responding with 0 (i.e. success). Some more digging into the usb-vhci code revealed that this error code corresponded to USB_VHCI_STATUS_CANCELED, and once I started responding with this I got the same behavior in my device as with the real device. Essentially I added this to my non-control section of process_urb:
/* if we have a INTERRUPT transfer */
if (usb_vhci_is_int(urb->type)) {
urb->status = USB_VHCI_STATUS_CANCELED;
return;
}
I'm not entirely out of the woods yet though. I noticed that the same thing seemed to apply for bulk IN transfers; I'm getting a ton of them during startup (which stop as soon as setup is complete) which - again - does not appear to be the case for the real device, and the real device - again - responds to these (superfluous) transfers with -ENOENT. I tried doing this, and it appears to work fine. The additional transfers do stop and it behaves just as the real device, but unfortunately it also results in my device not being able to send data back to the client. I modified my bulk IN handling code as follows:
/* if we have a BULK IN transfer */
if (usb_vhci_is_bulk(urb->type) && usb_vhci_is_in(urb->epadr)) {
if (last_command) {
// send version
memcpy(urb->buffer, VERSION_STR, strlen(VERSION_STR));
urb->buffer_actual = strlen(VERSION_STR);
last_command = 0;
urb->status = USB_VHCI_STATUS_SUCCESS;
} else {
urb->status = USB_VHCI_STATUS_CANCELED;
}
return;
}
I figure this should work, i.e. if I received a command in the previous bulk OUT transfer I should be able to use the IN transfer to respond (as I've been doing all along) and if there was no command I just respond with -ENOENT. For some reason this does not work though and I'm not sure why.
Another thing I noticed regarding the trace from the real device: although it does respond to these bulk transfers with -ENOENT, they send the response more than 10 seconds (!) after they received the request! Not sure what that's all about, but if anyone has an idea I'd be most grateful.
In my project, the requirement is to check the color of web element i.e red, green, pink etc.But using getCssValue() method are getting rgb value in hexadecimal form.Please help me to achieve it.Thank You.
One thing you can do is to write a script that will query a website like - http://name-of-color.com/ with the hex code and parse the html code and retrieve the Color name that is displayed on the website.
So, overall the steps would be:
Get the hex code (You already have it)
Query the website as follows (you could use the URL, URLConnection class for this OR you could use Selenium+PhantomJS OR Jsoup)
URL: http://name-of-color.com/?hex=#F89406
Scrape the html page source and get the color name that is displayed on the website
For getting the the name of the color you can use this class:
import java.awt.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
public class ColorUtils {
private static ArrayList<ColorName> initColorList() {
ArrayList<ColorName> colorList = new ArrayList<>();
colorList.add(new ColorName("AliceBlue", 0xF0, 0xF8, 0xFF));
colorList.add(new ColorName("AntiqueWhite", 0xFA, 0xEB, 0xD7));
colorList.add(new ColorName("Aqua", 0x00, 0xFF, 0xFF));
colorList.add(new ColorName("Aquamarine", 0x7F, 0xFF, 0xD4));
colorList.add(new ColorName("Azure", 0xF0, 0xFF, 0xFF));
colorList.add(new ColorName("Beige", 0xF5, 0xF5, 0xDC));
colorList.add(new ColorName("Bisque", 0xFF, 0xE4, 0xC4));
colorList.add(new ColorName("Black", 0x00, 0x00, 0x00));
colorList.add(new ColorName("BlanchedAlmond", 0xFF, 0xEB, 0xCD));
colorList.add(new ColorName("Blue", 0x00, 0x00, 0xFF));
colorList.add(new ColorName("BlueViolet", 0x8A, 0x2B, 0xE2));
colorList.add(new ColorName("Brown", 0xA5, 0x2A, 0x2A));
colorList.add(new ColorName("BurlyWood", 0xDE, 0xB8, 0x87));
colorList.add(new ColorName("CadetBlue", 0x5F, 0x9E, 0xA0));
colorList.add(new ColorName("Chartreuse", 0x7F, 0xFF, 0x00));
colorList.add(new ColorName("Chocolate", 0xD2, 0x69, 0x1E));
colorList.add(new ColorName("Coral", 0xFF, 0x7F, 0x50));
colorList.add(new ColorName("CornflowerBlue", 0x64, 0x95, 0xED));
colorList.add(new ColorName("Cornsilk", 0xFF, 0xF8, 0xDC));
colorList.add(new ColorName("Crimson", 0xDC, 0x14, 0x3C));
colorList.add(new ColorName("Cyan", 0x00, 0xFF, 0xFF));
colorList.add(new ColorName("DarkBlue", 0x00, 0x00, 0x8B));
colorList.add(new ColorName("DarkCyan", 0x00, 0x8B, 0x8B));
colorList.add(new ColorName("DarkGoldenRod", 0xB8, 0x86, 0x0B));
colorList.add(new ColorName("DarkGray", 0xA9, 0xA9, 0xA9));
colorList.add(new ColorName("DarkGreen", 0x00, 0x64, 0x00));
colorList.add(new ColorName("DarkKhaki", 0xBD, 0xB7, 0x6B));
colorList.add(new ColorName("DarkMagenta", 0x8B, 0x00, 0x8B));
colorList.add(new ColorName("DarkOliveGreen", 0x55, 0x6B, 0x2F));
colorList.add(new ColorName("DarkOrange", 0xFF, 0x8C, 0x00));
colorList.add(new ColorName("DarkOrchid", 0x99, 0x32, 0xCC));
colorList.add(new ColorName("DarkRed", 0x8B, 0x00, 0x00));
colorList.add(new ColorName("DarkSalmon", 0xE9, 0x96, 0x7A));
colorList.add(new ColorName("DarkSeaGreen", 0x8F, 0xBC, 0x8F));
colorList.add(new ColorName("DarkSlateBlue", 0x48, 0x3D, 0x8B));
colorList.add(new ColorName("DarkSlateGray", 0x2F, 0x4F, 0x4F));
colorList.add(new ColorName("DarkTurquoise", 0x00, 0xCE, 0xD1));
colorList.add(new ColorName("DarkViolet", 0x94, 0x00, 0xD3));
colorList.add(new ColorName("DeepPink", 0xFF, 0x14, 0x93));
colorList.add(new ColorName("DeepSkyBlue", 0x00, 0xBF, 0xFF));
colorList.add(new ColorName("DimGray", 0x69, 0x69, 0x69));
colorList.add(new ColorName("DodgerBlue", 0x1E, 0x90, 0xFF));
colorList.add(new ColorName("FireBrick", 0xB2, 0x22, 0x22));
colorList.add(new ColorName("FloralWhite", 0xFF, 0xFA, 0xF0));
colorList.add(new ColorName("ForestGreen", 0x22, 0x8B, 0x22));
colorList.add(new ColorName("Fuchsia", 0xFF, 0x00, 0xFF));
colorList.add(new ColorName("Gainsboro", 0xDC, 0xDC, 0xDC));
colorList.add(new ColorName("GhostWhite", 0xF8, 0xF8, 0xFF));
colorList.add(new ColorName("Gold", 0xFF, 0xD7, 0x00));
colorList.add(new ColorName("GoldenRod", 0xDA, 0xA5, 0x20));
colorList.add(new ColorName("Gray", 0x80, 0x80, 0x80));
colorList.add(new ColorName("Green", 0x00, 0x80, 0x00));
colorList.add(new ColorName("GreenYellow", 0xAD, 0xFF, 0x2F));
colorList.add(new ColorName("HoneyDew", 0xF0, 0xFF, 0xF0));
colorList.add(new ColorName("HotPink", 0xFF, 0x69, 0xB4));
colorList.add(new ColorName("IndianRed", 0xCD, 0x5C, 0x5C));
colorList.add(new ColorName("Indigo", 0x4B, 0x00, 0x82));
colorList.add(new ColorName("Ivory", 0xFF, 0xFF, 0xF0));
colorList.add(new ColorName("Khaki", 0xF0, 0xE6, 0x8C));
colorList.add(new ColorName("Lavender", 0xE6, 0xE6, 0xFA));
colorList.add(new ColorName("LavenderBlush", 0xFF, 0xF0, 0xF5));
colorList.add(new ColorName("LawnGreen", 0x7C, 0xFC, 0x00));
colorList.add(new ColorName("LemonChiffon", 0xFF, 0xFA, 0xCD));
colorList.add(new ColorName("LightBlue", 0xAD, 0xD8, 0xE6));
colorList.add(new ColorName("LightCoral", 0xF0, 0x80, 0x80));
colorList.add(new ColorName("LightCyan", 0xE0, 0xFF, 0xFF));
colorList.add(new ColorName("LightGoldenRodYellow", 0xFA, 0xFA, 0xD2));
colorList.add(new ColorName("LightGray", 0xD3, 0xD3, 0xD3));
colorList.add(new ColorName("LightGreen", 0x90, 0xEE, 0x90));
colorList.add(new ColorName("LightPink", 0xFF, 0xB6, 0xC1));
colorList.add(new ColorName("LightSalmon", 0xFF, 0xA0, 0x7A));
colorList.add(new ColorName("LightSeaGreen", 0x20, 0xB2, 0xAA));
colorList.add(new ColorName("LightSkyBlue", 0x87, 0xCE, 0xFA));
colorList.add(new ColorName("LightSlateGray", 0x77, 0x88, 0x99));
colorList.add(new ColorName("LightSteelBlue", 0xB0, 0xC4, 0xDE));
colorList.add(new ColorName("LightYellow", 0xFF, 0xFF, 0xE0));
colorList.add(new ColorName("Lime", 0x00, 0xFF, 0x00));
colorList.add(new ColorName("LimeGreen", 0x32, 0xCD, 0x32));
colorList.add(new ColorName("Linen", 0xFA, 0xF0, 0xE6));
colorList.add(new ColorName("Magenta", 0xFF, 0x00, 0xFF));
colorList.add(new ColorName("Maroon", 0x80, 0x00, 0x00));
colorList.add(new ColorName("MediumAquaMarine", 0x66, 0xCD, 0xAA));
colorList.add(new ColorName("MediumBlue", 0x00, 0x00, 0xCD));
colorList.add(new ColorName("MediumOrchid", 0xBA, 0x55, 0xD3));
colorList.add(new ColorName("MediumPurple", 0x93, 0x70, 0xDB));
colorList.add(new ColorName("MediumSeaGreen", 0x3C, 0xB3, 0x71));
colorList.add(new ColorName("MediumSlateBlue", 0x7B, 0x68, 0xEE));
colorList.add(new ColorName("MediumSpringGreen", 0x00, 0xFA, 0x9A));
colorList.add(new ColorName("MediumTurquoise", 0x48, 0xD1, 0xCC));
colorList.add(new ColorName("MediumVioletRed", 0xC7, 0x15, 0x85));
colorList.add(new ColorName("MidnightBlue", 0x19, 0x19, 0x70));
colorList.add(new ColorName("MintCream", 0xF5, 0xFF, 0xFA));
colorList.add(new ColorName("MistyRose", 0xFF, 0xE4, 0xE1));
colorList.add(new ColorName("Moccasin", 0xFF, 0xE4, 0xB5));
colorList.add(new ColorName("NavajoWhite", 0xFF, 0xDE, 0xAD));
colorList.add(new ColorName("Navy", 0x00, 0x00, 0x80));
colorList.add(new ColorName("OldLace", 0xFD, 0xF5, 0xE6));
colorList.add(new ColorName("Olive", 0x80, 0x80, 0x00));
colorList.add(new ColorName("OliveDrab", 0x6B, 0x8E, 0x23));
colorList.add(new ColorName("Orange", 0xFF, 0xA5, 0x00));
colorList.add(new ColorName("OrangeRed", 0xFF, 0x45, 0x00));
colorList.add(new ColorName("Orchid", 0xDA, 0x70, 0xD6));
colorList.add(new ColorName("PaleGoldenRod", 0xEE, 0xE8, 0xAA));
colorList.add(new ColorName("PaleGreen", 0x98, 0xFB, 0x98));
colorList.add(new ColorName("PaleTurquoise", 0xAF, 0xEE, 0xEE));
colorList.add(new ColorName("PaleVioletRed", 0xDB, 0x70, 0x93));
colorList.add(new ColorName("PapayaWhip", 0xFF, 0xEF, 0xD5));
colorList.add(new ColorName("PeachPuff", 0xFF, 0xDA, 0xB9));
colorList.add(new ColorName("Peru", 0xCD, 0x85, 0x3F));
colorList.add(new ColorName("Pink", 0xFF, 0xC0, 0xCB));
colorList.add(new ColorName("Plum", 0xDD, 0xA0, 0xDD));
colorList.add(new ColorName("PowderBlue", 0xB0, 0xE0, 0xE6));
colorList.add(new ColorName("Purple", 0x80, 0x00, 0x80));
colorList.add(new ColorName("Red", 0xFF, 0x00, 0x00));
colorList.add(new ColorName("RosyBrown", 0xBC, 0x8F, 0x8F));
colorList.add(new ColorName("RoyalBlue", 0x41, 0x69, 0xE1));
colorList.add(new ColorName("SaddleBrown", 0x8B, 0x45, 0x13));
colorList.add(new ColorName("Salmon", 0xFA, 0x80, 0x72));
colorList.add(new ColorName("SandyBrown", 0xF4, 0xA4, 0x60));
colorList.add(new ColorName("SeaGreen", 0x2E, 0x8B, 0x57));
colorList.add(new ColorName("SeaShell", 0xFF, 0xF5, 0xEE));
colorList.add(new ColorName("Sienna", 0xA0, 0x52, 0x2D));
colorList.add(new ColorName("Silver", 0xC0, 0xC0, 0xC0));
colorList.add(new ColorName("SkyBlue", 0x87, 0xCE, 0xEB));
colorList.add(new ColorName("SlateBlue", 0x6A, 0x5A, 0xCD));
colorList.add(new ColorName("SlateGray", 0x70, 0x80, 0x90));
colorList.add(new ColorName("Snow", 0xFF, 0xFA, 0xFA));
colorList.add(new ColorName("SpringGreen", 0x00, 0xFF, 0x7F));
colorList.add(new ColorName("SteelBlue", 0x46, 0x82, 0xB4));
colorList.add(new ColorName("Tan", 0xD2, 0xB4, 0x8C));
colorList.add(new ColorName("Teal", 0x00, 0x80, 0x80));
colorList.add(new ColorName("Thistle", 0xD8, 0xBF, 0xD8));
colorList.add(new ColorName("Tomato", 0xFF, 0x63, 0x47));
colorList.add(new ColorName("Turquoise", 0x40, 0xE0, 0xD0));
colorList.add(new ColorName("Violet", 0xEE, 0x82, 0xEE));
colorList.add(new ColorName("Wheat", 0xF5, 0xDE, 0xB3));
colorList.add(new ColorName("White", 0xFF, 0xFF, 0xFF));
colorList.add(new ColorName("WhiteSmoke", 0xF5, 0xF5, 0xF5));
colorList.add(new ColorName("Yellow", 0xFF, 0xFF, 0x00));
colorList.add(new ColorName("YellowGreen", 0x9A, 0xCD, 0x32));
return colorList;
}
public static String getColorNameFromRgb(int r, int g, int b) {
ArrayList<ColorName> colorList = initColorList();
ColorName closestMatch = null;
int minMSE = Integer.MAX_VALUE;
int mse;
for (ColorName c : colorList) {
mse = c.computeMSE(r, g, b);
if (mse < minMSE) {
minMSE = mse;
closestMatch = c;
}
}
if (closestMatch != null) {
return closestMatch.getName();
} else {
return "No matched color name.";
}
}
public static String getColorNameFromColor(Color color) {
return getColorNameFromRgb(color.getRed(), color.getGreen(),
color.getBlue());
}
public static Color stringToColor(final String value) {
if (value == null) {
return Color.black;
}
try {
return Color.decode(value);
} catch (NumberFormatException nfe) {
try {
final Field f = Color.class.getField(value);
return (Color) f.get(null);
} catch (Exception ce) {
return Color.black;
}
}
}
public static class ColorName {
public int r, g, b;
public String name;
public ColorName(String name, int r, int g, int b) {
this.r = r;
this.g = g;
this.b = b;
this.name = name;
}
public int computeMSE(int pixR, int pixG, int pixB) {
return ((pixR - r) * (pixR - r) + (pixG - g) * (pixG - g) + (pixB - b)
* (pixB - b)) / 3;
}
public String getName() {
return name;
}
}
}
And finally, the method should be like this:
public String getElementColor(WebElement element) {
String color = element.getCssValue("color");
String hex = Color.fromString(color).asHex();
java.awt.Color c = stringToColor(hex);
return getColorNameFromColor(c);
}
Swetha,
You will always get the color code with getCssValue(), if you want the color name, you could write a simple code to convert it. like i was refering one site for this, try this code-
string GetColorName(Color color)
{
var colorProperties = typeof(Color)
.GetProperties(BindingFlags.Public | BindingFlags.Static)
.Where(p => p.PropertyType == typeof(Color));
foreach(var colorProperty in colorProperties)
{
var colorPropertyValue = (Color)colorProperty.GetValue(null, null);
if(colorPropertyValue.R == color.R
&& colorPropertyValue.G == color.G
&& colorPropertyValue.B == color.B) {
return colorPropertyValue.Name;
}
}
//If unknown color, fallback to the hex value
//(or you could return null, "Unkown" or whatever you want)
return ColorTranslator.ToHtml(color);
}