Please explain me meaning of gsub("\047|,","",$0) in awk - bash

From source file, such as;
{'PRIMER_INTERNAL_EXPLAIN': 'considered 3, ok 3',
'PRIMER_INTERNAL_NUM_RETURNED': 0L,
'PRIMER_LEFT_0': (29L, 22L),
'PRIMER_LEFT_0_END_STABILITY': 3.86,
'PRIMER_LEFT_0_GC_PERCENT': 45.45454545454545,
'PRIMER_LEFT_0_HAIRPIN_TH': 0.0,
'PRIMER_LEFT_0_PENALTY': 1.1657103482262485,
'PRIMER_LEFT_0_SELF_ANY_TH': 0.0,
'PRIMER_LEFT_0_SELF_END_TH': 0.0,
'PRIMER_LEFT_0_SEQUENCE': 'ATGGCAAATACACAGAGGAAGC',
'PRIMER_LEFT_0_TM': 58.98043552542492,
'PRIMER_LEFT_1': (32L, 22L),
'PRIMER_LEFT_1_END_STABILITY': 4.35,
'PRIMER_LEFT_1_GC_PERCENT': 45.45454545454545,
'PRIMER_LEFT_1_HAIRPIN_TH': 31.75767174449885,
'PRIMER_LEFT_1_PENALTY': 1.2635420728922853,
'PRIMER_LEFT_1_SELF_ANY_TH': 0.0,
'PRIMER_LEFT_1_SELF_END_TH': 0.0,
'PRIMER_LEFT_1_SEQUENCE': 'GCAAATACACAGAGGAAGCCTT',
'PRIMER_LEFT_1_TM': 58.915214375647565,
'PRIMER_LEFT_2': (27L, 23L),
'PRIMER_LEFT_2_END_STABILITY': 3.46,
'PRIMER_LEFT_2_GC_PERCENT': 43.47826086956522,
'PRIMER_LEFT_2_HAIRPIN_TH': 0.0,
'PRIMER_LEFT_2_PENALTY': 1.396379766521477,
'PRIMER_LEFT_2_SELF_ANY_TH': 0.0,
'PRIMER_LEFT_2_SELF_END_TH': 0.0,
'PRIMER_LEFT_2_SEQUENCE': 'TGATGGCAAATACACAGAGGAAG',
'PRIMER_LEFT_2_TM': 58.735746822319015,
'PRIMER_LEFT_EXPLAIN': 'considered 479, overlap excluded region 66, GC content failed 18, low tm 100, high tm 139, ok 156',
'PRIMER_LEFT_NUM_RETURNED': 3L,
'PRIMER_PAIR_0_COMPL_ANY_TH': 0.0,
'PRIMER_PAIR_0_COMPL_END_TH': 0.0,
'PRIMER_PAIR_0_PENALTY': 3.6468465778591295,
'PRIMER_PAIR_0_PRODUCT_SIZE': 114L,
'PRIMER_PAIR_1_COMPL_ANY_TH': 0.0,
'PRIMER_PAIR_1_COMPL_END_TH': 0.0,
'PRIMER_PAIR_1_PENALTY': 3.744678302525166,
'PRIMER_PAIR_1_PRODUCT_SIZE': 111L,
'PRIMER_PAIR_2_COMPL_ANY_TH': 0.0,
'PRIMER_PAIR_2_COMPL_END_TH': 0.0,
'PRIMER_PAIR_2_PENALTY': 3.877515996154358,
'PRIMER_PAIR_2_PRODUCT_SIZE': 116L,
'PRIMER_PAIR_NUM_RETURNED': 3L,
'PRIMER_RIGHT_0': (142L, 22L),
'PRIMER_RIGHT_0_END_STABILITY': 3.33,
'PRIMER_RIGHT_0_GC_PERCENT': 40.90909090909091,
'PRIMER_RIGHT_0_HAIRPIN_TH': 39.90652312082375,
'PRIMER_RIGHT_0_PENALTY': 2.481136229632881,
'PRIMER_RIGHT_0_SELF_ANY_TH': 0.0,
'PRIMER_RIGHT_0_SELF_END_TH': 0.0,
'PRIMER_RIGHT_0_SEQUENCE': 'AGATGGTGAAACCTGTTTGTTG',
'PRIMER_RIGHT_0_TM': 57.345909180244746,
'PRIMER_RIGHT_1': (142L, 22L),
'PRIMER_RIGHT_1_END_STABILITY': 3.33,
'PRIMER_RIGHT_1_GC_PERCENT': 40.90909090909091,
'PRIMER_RIGHT_1_HAIRPIN_TH': 39.90652312082375,
'PRIMER_RIGHT_1_PENALTY': 2.481136229632881,
'PRIMER_RIGHT_1_SELF_ANY_TH': 0.0,
'PRIMER_RIGHT_1_SELF_END_TH': 0.0,
'PRIMER_RIGHT_1_SEQUENCE': 'AGATGGTGAAACCTGTTTGTTG',
'PRIMER_RIGHT_1_TM': 57.345909180244746,
'PRIMER_RIGHT_2': (142L, 22L),
'PRIMER_RIGHT_2_END_STABILITY': 3.33,
'PRIMER_RIGHT_2_GC_PERCENT': 40.90909090909091,
'PRIMER_RIGHT_2_HAIRPIN_TH': 39.90652312082375,
'PRIMER_RIGHT_2_PENALTY': 2.481136229632881,
'PRIMER_RIGHT_2_SELF_ANY_TH': 0.0,
'PRIMER_RIGHT_2_SELF_END_TH': 0.0,
'PRIMER_RIGHT_2_SEQUENCE': 'AGATGGTGAAACCTGTTTGTTG',
'PRIMER_RIGHT_2_TM': 57.345909180244746,
'PRIMER_RIGHT_EXPLAIN': 'considered 255, overlap excluded region 66, GC content failed 43, low tm 56, high tm 2, long poly-x seq 41, ok 47',
'PRIMER_RIGHT_NUM_RETURNED': 3L,
'SEQUENCE_ID': 'R-chr1:114713809-114714010',
'SEQUENCE_TEMPLATE': 'TAATATCCGCAAATGACTTGCTATTATTGATGGCAAATACACAGAGGAAGCCTTCGCCTGTCCTCATGTATTGGTCTCTCATGGCACTGTACTCTTCTTGTCCAGCTGTATCCAGTATGTCCAACAAACAGGTTTCACCATCTATAACCACTTGTTTTCTGTAAGAATCCTGGGGGTGTggagggtaagggggcagggagg'}
None
{'PRIMER_INTERNAL_EXPLAIN': 'considered 3, ok 3',
'PRIMER_INTERNAL_NUM_RETURNED': 0L,
'PRIMER_LEFT_0': (51L, 23L),
'PRIMER_LEFT_0_END_STABILITY': 4.24,
'PRIMER_LEFT_0_GC_PERCENT': 43.47826086956522,
'PRIMER_LEFT_0_HAIRPIN_TH': 0.0,
'PRIMER_LEFT_0_PENALTY': 1.11245483566546,
'PRIMER_LEFT_0_SELF_ANY_TH': 0.0,
'PRIMER_LEFT_0_SELF_END_TH': 0.0,
'PRIMER_LEFT_0_SEQUENCE': 'ACAAAGTGGTTCTGGATTAGCTG',
'PRIMER_LEFT_0_TM': 58.92503010955636,
'PRIMER_LEFT_1': (54L, 22L),
'PRIMER_LEFT_1_END_STABILITY': 3.86,
'PRIMER_LEFT_1_GC_PERCENT': 45.45454545454545,
'PRIMER_LEFT_1_HAIRPIN_TH': 0.0,
'PRIMER_LEFT_1_PENALTY': 1.2669303718815925,
'PRIMER_LEFT_1_SELF_ANY_TH': 0.0,
'PRIMER_LEFT_1_SELF_END_TH': 0.0,
'PRIMER_LEFT_1_SEQUENCE': 'AAGTGGTTCTGGATTAGCTGGA',
'PRIMER_LEFT_1_TM': 59.087044490345306,
'PRIMER_LEFT_2': (55L, 22L),
'PRIMER_LEFT_2_END_STABILITY': 3.41,
'PRIMER_LEFT_2_GC_PERCENT': 45.45454545454545,
'PRIMER_LEFT_2_HAIRPIN_TH': 0.0,
'PRIMER_LEFT_2_PENALTY': 1.3141607548431367,
'PRIMER_LEFT_2_SELF_ANY_TH': 0.0,
'PRIMER_LEFT_2_SELF_END_TH': 0.0,
'PRIMER_LEFT_2_SEQUENCE': 'AGTGGTTCTGGATTAGCTGGAT',
'PRIMER_LEFT_2_TM': 58.88146858768033,
'PRIMER_LEFT_EXPLAIN': 'considered 507, overlap excluded region 66, GC content failed 60, low tm 128, high tm 78, ok 175',
'PRIMER_LEFT_NUM_RETURNED': 3L,
'PRIMER_PAIR_0_COMPL_ANY_TH': 0.0,
'PRIMER_PAIR_0_COMPL_END_TH': 0.0,
'PRIMER_PAIR_0_PENALTY': 1.9448563397245948,
'PRIMER_PAIR_0_PRODUCT_SIZE': 102L,
'PRIMER_PAIR_1_COMPL_ANY_TH': 0.0,
'PRIMER_PAIR_1_COMPL_END_TH': 0.0,
'PRIMER_PAIR_1_PENALTY': 2.0993318759407273,
'PRIMER_PAIR_1_PRODUCT_SIZE': 99L,
'PRIMER_PAIR_2_COMPL_ANY_TH': 0.0,
'PRIMER_PAIR_2_COMPL_END_TH': 0.0,
'PRIMER_PAIR_2_PENALTY': 2.1465622589022715,
'PRIMER_PAIR_2_PRODUCT_SIZE': 98L,
'PRIMER_PAIR_NUM_RETURNED': 3L,
'PRIMER_RIGHT_0': (152L, 22L),
'PRIMER_RIGHT_0_END_STABILITY': 3.41,
'PRIMER_RIGHT_0_GC_PERCENT': 40.90909090909091,
'PRIMER_RIGHT_0_HAIRPIN_TH': 0.0,
'PRIMER_RIGHT_0_PENALTY': 0.8324015040591348,
'PRIMER_RIGHT_0_SELF_ANY_TH': 0.0,
'PRIMER_RIGHT_0_SELF_END_TH': 0.0,
'PRIMER_RIGHT_0_SEQUENCE': 'TTCTTGCTGGTGTGAAATGACT',
'PRIMER_RIGHT_0_TM': 58.44506566396058,
'PRIMER_RIGHT_1': (152L, 22L),
'PRIMER_RIGHT_1_END_STABILITY': 3.41,
'PRIMER_RIGHT_1_GC_PERCENT': 40.90909090909091,
'PRIMER_RIGHT_1_HAIRPIN_TH': 0.0,
'PRIMER_RIGHT_1_PENALTY': 0.8324015040591348,
'PRIMER_RIGHT_1_SELF_ANY_TH': 0.0,
'PRIMER_RIGHT_1_SELF_END_TH': 0.0,
'PRIMER_RIGHT_1_SEQUENCE': 'TTCTTGCTGGTGTGAAATGACT',
'PRIMER_RIGHT_1_TM': 58.44506566396058,
'PRIMER_RIGHT_2': (152L, 22L),
'PRIMER_RIGHT_2_END_STABILITY': 3.41,
'PRIMER_RIGHT_2_GC_PERCENT': 40.90909090909091,
'PRIMER_RIGHT_2_HAIRPIN_TH': 0.0,
'PRIMER_RIGHT_2_PENALTY': 0.8324015040591348,
'PRIMER_RIGHT_2_SELF_ANY_TH': 0.0,
'PRIMER_RIGHT_2_SELF_END_TH': 0.0,
'PRIMER_RIGHT_2_SEQUENCE': 'TTCTTGCTGGTGTGAAATGACT',
'PRIMER_RIGHT_2_TM': 58.44506566396058,
'PRIMER_RIGHT_EXPLAIN': 'considered 507, overlap excluded region 66, low tm 60, high tm 131, ok 250',
'PRIMER_RIGHT_NUM_RETURNED': 3L,
'SEQUENCE_ID': 'R-chr1:114716023-114716228',
'SEQUENCE_TEMPLATE': 'GTCAGCGGGCTACCACTGGGCCTCACCTCTATGGTGGGATCATATTCATCTACAAAGTGGTTCTGGATTAGCTGGATTGTCAGTGCGCTTTTCCCAACACCACCTGCTCCAACCACCACCAGTTTGTACTCAGTCATTTCACACCAGCAAGAACCTGTTGGAAACCAGTAATCAGGGTTAATTGGCGAGCCACATCTACAGTACT'}
None
If I using this,
grep -E "PRIMER_LEFT_0_SEQUENCE|PRIMER_LEFT_1_SEQUENCE|PRIMER_RIGHT_0_SEQUENCE|PRIMER_RIGHT_1_SEQUENCE|SEQUENCE_ID" p3out_22_59_3pairs_del_No_out.out | paste - - - - - | awk '{ gsub("\047|,","",$0); print ">"$10"-L0\n"$2"\n>"$10"-L1\n"$4"\n>"$10"-R0\n"$6"\n>"$10"-R1\n"$8}' > xgrep_2primers.out
Output is,
>R-chr1:114713809-114714010-L0
ATGGCAAATACACAGAGGAAGC
>R-chr1:114713809-114714010-L1
GCAAATACACAGAGGAAGCCTT
>R-chr1:114713809-114714010-R0
AGATGGTGAAACCTGTTTGTTG
>R-chr1:114713809-114714010-R1
AGATGGTGAAACCTGTTTGTTG
Can anyone please explain me the meaning of { gsub("\047|,","",$0) in awk? Awk is so powerful tool and I'd like to understand more. If you know good material or place to learn awk tool, please share it with me. Thanks in advance!

the \047 is an octal escape. it's actually just a single quote. when working with embedded quotes, it's sometimes easier to just write \047 rather than something like '\''.
as for gsub, it runs the regex '|, (the same as [',]) and deletes all matches (since the 2nd arg is an empty string).
check out the POSIX awk documentation where it describes gsub as:
gsub(ere, repl[, in])
Behave like sub (see below), except that it shall replace all
occurrences of the regular expression (like the ed utility global
substitute) in $0 or in the in argument, when specified.
if you're using gawk, check out the GNU awk manual.

It's the same as tr -d "'," Delete chars single quote and comma.

Replaces '| with nothing. A bare apostrophe would be the end of a string so is substituted with \047

Related

Plotly express boxplot image export - part of box colors missing in svg file

I have made a boxplot using plotly express (px.box), and the resulting plot looks good in my Google Chrome / Jupyter browser window. Here is a schreenshot of two randomly selected boxes and they look as I expect.
However, after exporting using pio.write_image, it looks like this (zoomed in):
WHY does it not fill up the whole box after export? What can I do to avoid it? I have tried defining width and height as "size*300" to set the DPI to 300, I have tried with and without "scale" and I have tried to use Orca as image export engine, tried export as .PDF, and updated Plotly (Plotly vers. 5.1.0). Links to comprehensive guides for export of high-quality plotly plots for use as figures in scientific papers also much appreciated, as exporting quality very often not satifying.
A example of the problem can be reproduced with this:
import plotly.express as px
import plotly.io as pio
import pandas as pd import plotly.graph_objs as go import sys import plotly
pio.templates.default = “simple_white”
x = [‘Cat1’, ‘Cat1’,‘Cat2’, ‘Cat2’, ‘Cat3’, ‘Cat3’, ‘Cat4’, ‘Cat4’,‘Cat5’, ‘Cat5’, ‘Cat6’, ‘Cat6’, ‘Cat6’, ‘Cat7’, ‘Cat7’, ‘Cat8’, ‘Cat8’,‘Cat11’, ‘Cat11’, ‘Cat12’,‘Cat12’, ‘Cat10’, ‘Cat10’, ‘Cat9’, ‘Cat9’, ‘Cat13’, ‘Cat13’, ‘Cat14’, ‘Cat14’, ‘Cat15’, ‘Cat15’, ‘Cat16’,‘Cat16’, ‘Cat17’]
y = [0.0, 0.0, 0.0, 0.0047, 0.0, 0.036, 0.0, 0.0, 0.12314, 0.02472495, 0.004,0.0, 0.013, 0.0, 0.0, 0.184, 0.056, 0.0186, 0.005928, 0.340, 0.20335, 0.0, 0.0, 0.2481, 0.12, 0.0, 0.0, 0.0201, 0.050, 0.0,0.0, 0.041, 0.0199, 0.0]
data = { “x”: x, “y”: y, }
df = pd.DataFrame(data)
box_plot = px.box(df, x=“x”, y=“y”, points=“all”, width=800, height=400) box_plot.update_yaxes(title=“Random numbers”, title_font=dict(size=18, family=‘Arial’), tickfont=dict(family=‘Arial’, size=18)) box_plot.update_xaxes(title=None, tickangle=45, title_font=dict(size=18, family=‘Arial’), tickfont=dict(family=‘Arial’, size=18), categoryorder=‘array’, categoryarray=[“Cat2”, “Cat1”, “Cat3”,“Cat4”, “Cat5”, “Cat6”, “Cat10”, “Cat11”, “Cat12”,“Cat9”, “Cat8”, “Cat7”, “Cat13”, “Cat14”, “Cat15”,“Cat16”, “Cat17”]) box_plot.update_layout(margin=dict(l = 40, r = 10, t = 10, b = 25), width=1100, height=400, font_family=“Arial”) box_plot.update_traces(boxmean=“sd”, selector=dict(type=‘box’)) box_plot.update_traces(pointpos=-2, selector=dict(type=‘box’)) box_plot.update_traces(marker_symbol=“circle-open”, selector=dict(type=‘box’)) box_plot.show()
pio.write_image(box_plot, r"Boxplot_minimal_work_ex.svg")
I tested first with only two categories, and the export file looked fine! But when I increase the number of categories, it makes the bad quality graph. I wonder if there is an influence from setting the width - so I tried to delete the width and heigth setting from the px.box expression but it gave same bad result.

Scrape JSON response with Selenium Browser [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I want to make the following actions automatically:
I open a web page with Google Chrome.
wait for it to render all the needed information.
go to Inspect Element, tab Network, and look at XHR requests.
find the file that I need.
copy the content of its response (to save it in a txt file).
It's kind of web scraping, but with less effort (how I think).
The problem is that I can't find what tools allow me to do that.
I started with Python and Selenium (chrome driver). But didn't found any info, is it possible to get XHR responses or not. All the tutorials are about scraping HTML. It seems logic to be possible, but my research didn't help.
Any idea?
Thank you.
The website you are trying to scrape has a dynamically generated content by JavaScript .
You have two options to work your way around that
Simulate a human browser interaction using selenium and open the website then wait till all the content is rendered and then use selenium to Extract the data you seek . this approach deals with the Elements tab. you just use css or xpath selectors to get the tags you want
instead of finding a way to make selenium go to network tab and save the content ( which you will find extremely hard to do ) you should get the URL of the XHR request and build the same request with the same headers and parameters if any exists and then use requests to send that request and you can save the content easily .
Let's try to scrape Home | Microsoft Academic
First approach :
from selenium import webdriver
driver = webdriver.Chrome() # Launch the browser
driver.get("https://academic.microsoft.com/home") # Go to the given url
authors = driver.find_elements_by_xpath('//a[#data-appinsights-action="TopAuthorSelected"]') # get the elements using selectors
for author in authors: # loop through them
print(author.text)
Output :
1. Yoshua Bengio
2. Geoffrey E. Hinton
3. Andrew Zisserman
4. Ilya Sutskever
5. Jian Sun
6. Trevor Darrell
7. Scott Shenker
8. Jiawei Han
9. Kaiming He
10. Ross Girshick
11. Ion Stoica
12. Hari Balakrishnan
13. R Core Team
14. Jitendra Malik
15. Jeffrey Dean
Second approach :
import requests
res = requests.get('https://academic.microsoft.com/api/analytics/authors/topauthors?topicId=41008148&take=15&filter=1&dateRange=1').json()
#The XHR Response is Usually in Json format
#res = [{'name': 'Yoshua Bengio', 'id': '161269817', 'lat': 0.0, 'lon': 0.0}, {'name': 'Geoffrey E. Hinton', 'id': '563069026', 'lat': 0.0, 'lon': 0.0}, {'name': 'Andrew Zisserman', 'id': '2469405535', 'lat': 0.0, 'lon': 0.0}, {'name': 'Ilya Sutskever', 'id': '215131072', 'lat': 0.0, 'lon': 0.0}, {'name': 'Jian Sun', 'id': '2200192130', 'lat': 0.0, 'lon': 0.0}, {'name': 'Trevor Darrell', 'id': '2174985400', 'lat': 0.0, 'lon': 0.0}, {'name': 'Scott Shenker', 'id': '719828399', 'lat': 0.0, 'lon': 0.0}, {'name': 'Jiawei Han', 'id': '2121939561', 'lat': 0.0, 'lon': 0.0}, {'name': 'Kaiming He', 'id': '2164292938', 'lat': 0.0, 'lon': 0.0}, {'name': 'Ross Girshick', 'id': '2473549963', 'lat': 0.0, 'lon': 0.0}, {'name': 'Ion Stoica', 'id': '2161479384', 'lat': 0.0, 'lon': 0.0}, {'name': 'Hari Balakrishnan', 'id': '1998464616', 'lat': 0.0, 'lon': 0.0}, {'name': 'R Core Team', 'id': '2976715238', 'lat': 0.0, 'lon': 0.0}, {'name': 'Jitendra Malik', 'id': '2136556746', 'lat': 0.0, 'lon': 0.0}, {'name': 'Jeffrey Dean', 'id': '2429370538', 'lat': 0.0, 'lon': 0.0}]
for author in res:
print(author['name'])
Output:
Yoshua Bengio
Geoffrey E. Hinton
Andrew Zisserman
Ilya Sutskever
Jian Sun
Trevor Darrell
Scott Shenker
Jiawei Han
Kaiming He
Ross Girshick
Ion Stoica
Hari Balakrishnan
R Core Team
Jitendra Malik
Jeffrey Dean
Second approach saves time , resources and straight forward .
Using First approach Image
Using Second approach Image
BrowserMob proxy (https://github.com/lightbody/browsermob-proxy) will help you with this. It will capture all requests, and when configured, their responses.
See this previous answer for more details: Running Selenium Webdriver with a proxy in Python

Resolving pytorch distributed execution printing multiple log statements for each process spawned?

I am running pytorch distributed environment to train some models and in the same script I am also using logging to print status of the program. The problem is that with pytorch distributed since its spawning multiple processes I see my log statements being printed n times where n is the number of processes being spawned. Here's an example of it:
1.0, 0.05, 2.1823, 0.1703, 1.9799, 0.2352
1.0, 0.05, 2.1804, 0.1674, 1.9767, 0.2406
1.0, 0.05, 2.1814, 0.1697, 2.0053, 0.2154
2.0, 0.05, 2.1593, 0.1741, 2.0935, 0.192
2.0, 0.05, 2.1526, 0.1779, 2.1166, 0.1908
2.0, 0.05, 2.1562, 0.1812, 2.0868, 0.2076
3.0, 0.05, 1.9319, 0.2473, 1.8041, 0.2903
3.0, 0.05, 1.9386, 0.2413, 1.8037, 0.3017
3.0, 0.05, 1.9286, 0.2443, 1.815, 0.2939
4.0, 0.05, 1.7522, 0.3153, 1.828, 0.3131
4.0, 0.05, 1.7504, 0.3207, 1.7613, 0.3245
4.0, 0.05, 1.7522, 0.3223, 1.7841, 0.3209
5.0, 0.05, 1.5815, 0.3951, 1.5559, 0.4307
5.0, 0.05, 1.5767, 0.3939, 1.5326, 0.4205
5.0, 0.05, 1.588, 0.3909, 1.5882, 0.3995
Any ideas on how to avoid or resolve this issue? Thanks!
You can choose to use NVIDIA pytorch scripts, it's optimized, which means it runs fast and print log normally.
here is the link:
https://github.com/NVIDIA/DeepLearningExamples/tree/master/PyTorch

How to change text colors through code in Xcode

I am trying to make a button that changes the color of the text and shadow in a label called labelDisplay in Xcode. I have tried using the following code inside a button's press function:
labelDisplay.textColor = UIColor(displayP3Red: 1, green: 1, blue: 1, alpha: 1)
labelDisplay.shadowColor = UIColor(displayP3Red: 60, green: 60, blue: 60, alpha: 1)
and in another button:
labelDisplay.textColor = UIColor(displayP3Red: 183, green: 181, blue: 60, alpha: 1)
labelDisplay.shadowColor = UIColor(displayP3Red: 145, green: 143, blue: 52, alpha: 1)
I don't know why, but when I go into the simulator and press any of these buttons, this error appears in the output box:
2018-04-20 14:09:12.641080 HelloWorld_Brandon[2362:34953] [Graphics] UIColor created with component values far outside the expected range. Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once.
Can anyone help me with this?
Are you using the Display P3 color space intentionally?
Either way, the arguments are "specified as a value from 0.0 to 1.0" according to the docs, and 183 is way bigger than that. Try specifying 0.71 or 183.0/255.0 instead.

How to Fill a path with a pattern in Quartz

I have created a view and in my draw rect method I create paths depending on what a user does with sliders. Using standard colors , everything works and looks very nice. I am trying to follow a code snippet from apple that shows how to draw patterns into a rect at this link:
Apple Drawing Guide
The example shows how to create a function callback with the pattern desired and then an additional method call to draw the rect. If I call the code as it is written from my rect it will draw my pattern as I would expect, however, I do not want to fill my rect , I want to fill a specified path in the rect. If I change the call in the drawing method from CGContextFillRect to CGContextFillPath, it doesn't work. I'm sure there is something I am overlooking to modify this code to get it to do what I want.
My callback pattern is a simple checkerboard:
code:
// Call Back function for Graphics Pattern
#define PATTERN_SIZE 10
void patternSpec(void *info , CGContextRef pContext){
NSLog(#"patternSpec Callback Called");
CGFloat subUnit = PATTERN_SIZE / 2;
CGRect square1 = {{0,0}, {subUnit, subUnit}},
square2 = {{subUnit, subUnit}, {subUnit, subUnit}},
square3 = {{0 , subUnit}, {subUnit, subUnit}},
square4 = {{subUnit , 0}, {subUnit, subUnit}};
CGContextSetRGBFillColor(pContext, 1.0, 0.0, 0.0, 1.0 );
CGContextFillRect(pContext, square1);
CGContextSetRGBFillColor(pContext, 1.0, 0.0, 0.0, 1.0 );
CGContextFillRect(pContext, square2);
CGContextSetRGBFillColor(pContext, 0.0, 0.0, 0.0, 1.0 );
CGContextFillRect(pContext, square3);
CGContextSetRGBFillColor(pContext, 0.0, 0.0, 0.0, 1.0 );
CGContextFillRect(pContext, square4);
}
// Method that draws the pattern
static void drawPattern (CGContextRef myContext)
{
NSLog(#"drawPattern Called ");
CGPatternRef pattern;
CGColorSpaceRef patternSpace;
CGFloat alpha = 1.0;
//width, height;
static const CGPatternCallbacks callbacks = {0, &patternSpec, NULL};
CGContextSaveGState (myContext);
patternSpace = CGColorSpaceCreatePattern (NULL);// 6
CGContextSetFillColorSpace (myContext, patternSpace);// 7
CGColorSpaceRelease (patternSpace);// 8
pattern = CGPatternCreate (NULL,CGRectMake (0, 0, PATTERN_SIZE, PATTERN_SIZE),
CGAffineTransformIdentity, PATTERN_SIZE, PATTERN_SIZE,
kCGPatternTilingConstantSpacing true, &callbacks);
CGContextSetFillPattern (myContext, pattern, &alpha);// 17
CGPatternRelease (pattern);// 18
//CGContextFillRect(myContext, rect);
CGContextDrawPath(myContext, kCGPathFill);
CGContextRestoreGState (myContext);
}
Here is a snippet of the code where I would like to call the routine:
CGContextSetLineWidth(context, .7);
CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0);
// Standard non-inverted view scenario.
CGContextBeginPath(context);
CGContextMoveToPoint(context, 0.00 , bMargin);
CGContextAddLineToPoint(context, highPX - curveSP , bMargin);
[self addCurve:context startX:highPX startY:bMargin radius:bo curveSp:curveSP curveDir:FL_BL];
CGContextAddLineToPoint(context, highPX, ((h - tMargin) - curveSP) );
[self addCurve:context startX:highPX startY: (h - tMargin) radius:bo curveSp:curveSP curveDir:FL_TL];
CGContextAddLineToPoint(context, (lowPX - curveSP), (h - tMargin) );
[self addCurve:context startX: lowPX startY: (h - tMargin) radius:bo curveSp:curveSP curveDir:FL_TR];
CGContextAddLineToPoint(context, lowPX, (bMargin + curveSP) );
[self addCurve:context startX:lowPX startY: bMargin radius:bo curveSp:curveSP curveDir:FL_BR];
CGContextAddLineToPoint(context, w, bMargin);
//CGContextDrawPath(context, nonInvertedView);
CGContextDrawPath(context, kCGPathStroke);
// fill with pattern
drawPattern(context);
The actual apple example also includes an NSRect arg in the drawing method, but since I don't want to fill a rect, I figured I could omit that. not sure though.
Thanks
CGContextDrawPath resets the current path. (They used to mention that somewhere, but I couldn't find it in a quick search.)
Save the graphics state before you stroke, then restore before you fill with the pattern.
(I assume you're specifically trying to get an outer stroke by stroking and then filling over half of it. If you want or can accept a centered stroke, kCGPathFillStroke will do the job with a single CGContextDrawPath call.)
So heres an update: I don't fully understand whats happening, but if I drop the code in the drawPattern method into a test app with an empty rect, it draws just like it should. If I drop the code into my method for drawing a path into a view , I get very strange behavior; it even tries to redraw parts of the view controller it should't even know about.
As soon as I deleted the CGContextSaveGState() , CGColorSpaceRelease(), CGPatternRelease(), and the CGContextRestoreGState(), the code started doing exactly what I wanted. I modified the method to this:
static void drawPattern(CGContextRef *pContext){
static CGPatternRef pattern;
static CGColorSpaceRef patternSpace;
static CGFloat alpha = 1.0;
static const CGPatternCallbacks callbacks = {0, &patternSpec, NULL};
patternSpace = CGColorSpaceCreatePattern (NULL);
CGContextSetFillColorSpace (pContext, patternSpace);
pattern = CGPatternCreate (NULL,
CGRectMake (0, 0, PATTERN_SIZE, PATTERN_SIZE),
CGAffineTransformIdentity,
PATTERN_SIZE,
PATTERN_SIZE,
kCGPatternTilingConstantSpacing,
true, &callbacks);
CGContextSetFillPattern (pContext, pattern, &alpha);
}
Now I can either call the defined pattern, or set a define fill color:
CGContextSetFillColor(context);
or:
drawPattern(context);
I would appreciate input on this, because I would like to know if leaving out some of these saveState or Release methods is a problem such as memory leaks.
Thanks

Resources